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