In this short guide, I will tell you how to use Spreadshirt's shop, country and currency data to format your prices correctly.
Formatting Rules and Data
For formatting a price we need the following 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 data from? A shop, such as shop 205909, is actually configured for a specific country and currency. A sample payload is illustrated below:
<?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="http://api.spreadshirt.net/api/v1/shops/205909" id="205909"> <user xlink:href="http://api.spreadshirt.net/api/v1/users/40000" id="40000"/> <country xlink:href="http://api.spreadshirt.net/api/v1/countries/1" id="1"/> <language xlink:href="http://api.spreadshirt.net/api/v1/languages/1" id="1"/> <currency xlink:href="http://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.
<?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="http://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.
<?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="http://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="http://api.spreadshirt.net/api/v1/currencies/1" id="1"/> </country>
Example
<?php // This script shows you how to format prices using Spreadshirt API v1 and // Spreadshirt's shop, currency and country data. // 1. Get shop $shopUrl = "http://api.spreadshirt.net/api/v1/shops/205909"; $ch = curl_init($shopUrl); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $result = curl_exec($ch); // Close the handle curl_close($ch); $shop = new SimpleXMLElement($result); $namespaces = $shop->getNamespaces(true); // 2. Get currency $attributes = $shop->currency->attributes($namespaces['xlink']); $currencyUrl = $attributes->href; $ch = curl_init($currencyUrl); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $result = curl_exec($ch); // Close the handle curl_close($ch); $currency = new SimpleXMLElement($result); // 3. Get country $attributes = $shop->country->attributes($namespaces['xlink']); $countryUrl = $attributes->href; $ch = curl_init($countryUrl); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); $result = curl_exec($ch); // Close the handle curl_close($ch); $country = new SimpleXMLElement($result); // 4. calculate the price $price = formatPrice("19.3", $currency->symbol, $currency->decimalCount, $currency->pattern, $country->thousandsSeparator, $country->decimalPoint); // 5. print the price echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body>'; echo $price; echo '</body></html>'; 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; }; ?>