Wiki source code of Price Formatting Guide
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | In this short guide, I will tell you how to use Spreadshirt's shop, country and currency data to format your prices correctly. | ||
2 | |||
3 | = Formatting Rules and Data = | ||
4 | |||
5 | For formatting a price we need the following data: | ||
6 | |||
7 | * **thousands separator** - . or , depending on the country | ||
8 | * **decimal point** - , or . depending on the country | ||
9 | * **currency symbol** - € or other depending on the currency | ||
10 | * **price pattern** - currency symbol before or after price depending on the currency | ||
11 | * **decimal count** - 1 or 2 decimal counts depending on the currency | ||
12 | |||
13 | Where do we get this data from? A shop, such as [[shop 205909>>url:https://api.spreadshirt.net/api/v1/shops/205909||shape="rect"]], is actually configured for a specific country and currency. A sample payload is illustrated below: | ||
14 | |||
15 | {{code}} | ||
16 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
17 | <shop xmlns:xlink="http://www.w3.org/1999/xlink" | ||
18 | xmlns="http://api.spreadshirt.net" | ||
19 | xlink:href="https://api.spreadshirt.net/api/v1/shops/205909" id="205909"> | ||
20 | <user xlink:href="https://api.spreadshirt.net/api/v1/users/40000" id="40000"/> | ||
21 | <country xlink:href="https://api.spreadshirt.net/api/v1/countries/1" id="1"/> | ||
22 | <language xlink:href="https://api.spreadshirt.net/api/v1/languages/1" id="1"/> | ||
23 | <currency xlink:href="https://api.spreadshirt.net/api/v1/currencies/1" id="1"/> | ||
24 | ... | ||
25 | </shop> | ||
26 | |||
27 | {{/code}} | ||
28 | |||
29 | The **currency**, such as [[currency 1>>url:https://api.spreadshirt.net/api/v1/currencies/1||shape="rect"]] defines the **currency symbol**, the **price pattern** and the **decimal count**. | ||
30 | |||
31 | {{code}} | ||
32 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
33 | <currency xmlns:xlink="http://www.w3.org/1999/xlink" | ||
34 | xmlns="http://api.spreadshirt.net" | ||
35 | xlink:href="https://api.spreadshirt.net/api/v1/currencies/1" id="1"> | ||
36 | <plain>EUR</plain> | ||
37 | <isoCode>EUR</isoCode> | ||
38 | <symbol>€</symbol> | ||
39 | <decimalCount>2</decimalCount> | ||
40 | <pattern>% $</pattern> | ||
41 | </currency> | ||
42 | |||
43 | {{/code}} | ||
44 | |||
45 | The **country**, such as [[country 1>>url:https://api.spreadshirt.net/api/v1/countries/1||shape="rect"]] defines the **thousands separator** and the **decimal point**. | ||
46 | |||
47 | {{code}} | ||
48 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
49 | <country xmlns:xlink="http://www.w3.org/1999/xlink" | ||
50 | xmlns="http://api.spreadshirt.net" | ||
51 | xlink:href="https://api.spreadshirt.net/api/v1/countries/1" id="1"> | ||
52 | <isoCode>DE</isoCode> | ||
53 | <thousandsSeparator>.</thousandsSeparator> | ||
54 | <decimalPoint>,</decimalPoint> | ||
55 | <length> | ||
56 | <unit>cm</unit> | ||
57 | <unitFactor>1.0</unitFactor> | ||
58 | </length> | ||
59 | <currency xlink:href="https://api.spreadshirt.net/api/v1/currencies/1" id="1"/> | ||
60 | </country> | ||
61 | |||
62 | {{/code}} | ||
63 | |||
64 | = Example = | ||
65 | |||
66 | {{code}} | ||
67 | <?php | ||
68 | // This script shows you how to format prices using Spreadshirt API v1 and | ||
69 | // Spreadshirt's shop, currency and country data. | ||
70 | |||
71 | // 1. Get shop | ||
72 | $shopUrl = "http://api.spreadshirt.net/api/v1/shops/205909"; | ||
73 | $ch = curl_init($shopUrl); | ||
74 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
75 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
76 | curl_setopt($ch, CURLOPT_HEADER, false); | ||
77 | $result = curl_exec($ch); | ||
78 | // Close the handle | ||
79 | curl_close($ch); | ||
80 | |||
81 | $shop = new SimpleXMLElement($result); | ||
82 | $namespaces = $shop->getNamespaces(true); | ||
83 | |||
84 | // 2. Get currency | ||
85 | $attributes = $shop->currency->attributes($namespaces['xlink']); | ||
86 | $currencyUrl = $attributes->href; | ||
87 | $ch = curl_init($currencyUrl); | ||
88 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
89 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
90 | curl_setopt($ch, CURLOPT_HEADER, false); | ||
91 | $result = curl_exec($ch); | ||
92 | // Close the handle | ||
93 | curl_close($ch); | ||
94 | |||
95 | $currency = new SimpleXMLElement($result); | ||
96 | |||
97 | // 3. Get country | ||
98 | $attributes = $shop->country->attributes($namespaces['xlink']); | ||
99 | $countryUrl = $attributes->href; | ||
100 | $ch = curl_init($countryUrl); | ||
101 | curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); | ||
102 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
103 | curl_setopt($ch, CURLOPT_HEADER, false); | ||
104 | $result = curl_exec($ch); | ||
105 | // Close the handle | ||
106 | curl_close($ch); | ||
107 | |||
108 | $country = new SimpleXMLElement($result); | ||
109 | |||
110 | // 4. calculate the price | ||
111 | $price = formatPrice("19.3", $currency->symbol, $currency->decimalCount, $currency->pattern, | ||
112 | $country->thousandsSeparator, $country->decimalPoint); | ||
113 | |||
114 | // 5. print the price | ||
115 | echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body>'; | ||
116 | echo $price; | ||
117 | echo '</body></html>'; | ||
118 | |||
119 | function formatPrice($price, $symbol, $decimalCount, $pattern, $thousandsSeparator, $decimalPoint) { | ||
120 | // formatting settings | ||
121 | $price = "" . $price; | ||
122 | |||
123 | // split integer from cents | ||
124 | $centsVal = ""; | ||
125 | $integerVal = "0"; | ||
126 | if (strpos($price, '.') != -1) { | ||
127 | $centsVal = "" .substr($price, strpos($price, '.') + 1, strlen($price) - strpos($price, '.') + 1); | ||
128 | $integerVal = "" .substr($price, 0, strpos($price, '.')); | ||
129 | } else { | ||
130 | $integerVal = $price; | ||
131 | } | ||
132 | |||
133 | $formatted = ""; | ||
134 | |||
135 | $count = 0; | ||
136 | for ($j = strlen($integerVal)-1; $j >= 0; $j--) { | ||
137 | $character = $integerVal[$j]; | ||
138 | $count++; | ||
139 | if ($count % 3 == 0) | ||
140 | $formatted = ($thousandsSeparator . $character) . $formatted; | ||
141 | else | ||
142 | $formatted = $character . $formatted; | ||
143 | } | ||
144 | if ($formatted[0] == $thousandsSeparator) | ||
145 | $formatted = substr($formatted, 1, strlen($formatted)); | ||
146 | |||
147 | $formatted .= $decimalPoint; | ||
148 | |||
149 | for ($j = 0; $j < $decimalCount; $j++) { | ||
150 | if($j < strlen($centsVal)) { | ||
151 | $formatted .= "" . $centsVal[$j]; | ||
152 | } else { | ||
153 | $formatted .= "0"; | ||
154 | } | ||
155 | } | ||
156 | |||
157 | $out = $pattern; | ||
158 | $out = str_replace('%', $formatted, $out); | ||
159 | $out = str_replace('$', $symbol, $out); | ||
160 | return $out; | ||
161 | }; | ||
162 | ?> | ||
163 | |||
164 | {{/code}} |