Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this short guide, I we will tell you how to use Spreadshirt's shop, country and currency data to format your prices correctly. This is only useful for advanced, generic implementations. If you maintain an implementation used for a single shop, you can hard code whatever works for you.

Formatting Rules and Data

...

  • thousands separator - . or , depending on the country
  • decimal point - , or . depending on the country
  • currency symbol - € or other depending on the currency
  • price pattern - currency symbol before or after price depending on the currency
  • decimal count - 1 or 2 decimal counts depending on the currency

Where do we get this the data from? A shop, such as shop 205909, is actually configured for a specific country and currency. A sample payload is illustrated below:

Code Block
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<shop xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns="http://api.spreadshirt.net"
      xlink:href="https://api.spreadshirt.net/api/v1/shops/205909" id="205909">
    <user xlink:href="https://api.spreadshirt.net/api/v1/users/40000" id="40000"/>
    <country xlink:href="https://api.spreadshirt.net/api/v1/countries/1" id="1"/>
    <language xlink:href="https://api.spreadshirt.net/api/v1/languages/1" id="1"/>
    <currency xlink:href="https://api.spreadshirt.net/api/v1/currencies/1" id="1"/>
    ...
</shop>

The currency, such as currency 1 defines the currency symbol, the price pattern and the decimal count.

Code Block
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<currency xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns="http://api.spreadshirt.net"
          xlink:href="https://api.spreadshirt.net/api/v1/currencies/1" id="1">
    <plain>EUR</plain>
    <isoCode>EUR</isoCode>
    <symbol>€</symbol>
    <decimalCount>2</decimalCount>
    <pattern>% $</pattern>
</currency>

The country, such as country 1 defines the thousands separator and the decimal point.

Code Block
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<country xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns="http://api.spreadshirt.net"
         xlink:href="https://api.spreadshirt.net/api/v1/countries/1" id="1">
    <isoCode>DE</isoCode>
    <thousandsSeparator>.</thousandsSeparator>
    <decimalPoint>,</decimalPoint>
    <length>
        <unit>cm</unit>
        <unitFactor>1.0</unitFactor>
    </length>
    <currency xlink:href="https://api.spreadshirt.net/api/v1/currencies/1" id="1"/>
</country>

Example

...

Fetch your shop and fetch country and currency accordingly.

Example

Code Block
<?php
    function formatPrice($price, $symbol, $decimalCount, $pattern, $thousandsSeparator, $decimalPoint) {
        // formatting settings
        $price = "" . $price;

        // split integer from cents
        $centsVal = "";
        $integerVal = "0";
        if (strpos($price, '.') != -1) {
            $centsVal = "" .substr($price, strpos($price, '.') + 1, strlen($price) - strpos($price, '.') + 1);
            $integerVal = "" .substr($price, 0, strpos($price, '.'));
        } else {
            $integerVal = $price;
        }

        $formatted = "";

        $count = 0;
        for ($j = strlen($integerVal)-1; $j >= 0; $j--) {
            $character = $integerVal[$j];
            $count++;
            if ($count % 3 == 0)
                $formatted = ($thousandsSeparator . $character) . $formatted;
            else
                $formatted = $character . $formatted;
        }
        if ($formatted[0] == $thousandsSeparator)
            $formatted = substr($formatted, 1, strlen($formatted));

        $formatted .= $decimalPoint;

        for ($j = 0; $j < $decimalCount; $j++) {
            if($j < strlen($centsVal)) {
                $formatted .= "" . $centsVal[$j];
            } else {
                $formatted .= "0";
            }
        }

        $out = $pattern;
        $out = str_replace('%', $formatted, $out);
        $out = str_replace('$', $symbol, $out);
        return $out;
    };
?>

...