1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 class WC_Countries {
14
15
16 public $countries;
17
18
19 public $states;
20
21
22 public $locale;
23
24
25 public $address_formats;
26
27 28 29 30 31 32
33 public function __construct() {
34 global $woocommerce, $states;
35
36 $this->countries = apply_filters( 'woocommerce_countries', array(
37 'AF' => __( 'Afghanistan', 'woocommerce' ),
38 'AX' => __( 'Åland Islands', 'woocommerce' ),
39 'AL' => __( 'Albania', 'woocommerce' ),
40 'DZ' => __( 'Algeria', 'woocommerce' ),
41 'AD' => __( 'Andorra', 'woocommerce' ),
42 'AO' => __( 'Angola', 'woocommerce' ),
43 'AI' => __( 'Anguilla', 'woocommerce' ),
44 'AQ' => __( 'Antarctica', 'woocommerce' ),
45 'AG' => __( 'Antigua and Barbuda', 'woocommerce' ),
46 'AR' => __( 'Argentina', 'woocommerce' ),
47 'AM' => __( 'Armenia', 'woocommerce' ),
48 'AW' => __( 'Aruba', 'woocommerce' ),
49 'AU' => __( 'Australia', 'woocommerce' ),
50 'AT' => __( 'Austria', 'woocommerce' ),
51 'AZ' => __( 'Azerbaijan', 'woocommerce' ),
52 'BS' => __( 'Bahamas', 'woocommerce' ),
53 'BH' => __( 'Bahrain', 'woocommerce' ),
54 'BD' => __( 'Bangladesh', 'woocommerce' ),
55 'BB' => __( 'Barbados', 'woocommerce' ),
56 'BY' => __( 'Belarus', 'woocommerce' ),
57 'BE' => __( 'Belgium', 'woocommerce' ),
58 'PW' => __( 'Belau', 'woocommerce' ),
59 'BZ' => __( 'Belize', 'woocommerce' ),
60 'BJ' => __( 'Benin', 'woocommerce' ),
61 'BM' => __( 'Bermuda', 'woocommerce' ),
62 'BT' => __( 'Bhutan', 'woocommerce' ),
63 'BO' => __( 'Bolivia', 'woocommerce' ),
64 'BQ' => __( 'Bonaire, Saint Eustatius and Saba', 'woocommerce' ),
65 'BA' => __( 'Bosnia and Herzegovina', 'woocommerce' ),
66 'BW' => __( 'Botswana', 'woocommerce' ),
67 'BV' => __( 'Bouvet Island', 'woocommerce' ),
68 'BR' => __( 'Brazil', 'woocommerce' ),
69 'IO' => __( 'British Indian Ocean Territory', 'woocommerce' ),
70 'VG' => __( 'British Virgin Islands', 'woocommerce' ),
71 'BN' => __( 'Brunei', 'woocommerce' ),
72 'BG' => __( 'Bulgaria', 'woocommerce' ),
73 'BF' => __( 'Burkina Faso', 'woocommerce' ),
74 'BI' => __( 'Burundi', 'woocommerce' ),
75 'KH' => __( 'Cambodia', 'woocommerce' ),
76 'CM' => __( 'Cameroon', 'woocommerce' ),
77 'CA' => __( 'Canada', 'woocommerce' ),
78 'CV' => __( 'Cape Verde', 'woocommerce' ),
79 'KY' => __( 'Cayman Islands', 'woocommerce' ),
80 'CF' => __( 'Central African Republic', 'woocommerce' ),
81 'TD' => __( 'Chad', 'woocommerce' ),
82 'CL' => __( 'Chile', 'woocommerce' ),
83 'CN' => __( 'China', 'woocommerce' ),
84 'CX' => __( 'Christmas Island', 'woocommerce' ),
85 'CC' => __( 'Cocos (Keeling) Islands', 'woocommerce' ),
86 'CO' => __( 'Colombia', 'woocommerce' ),
87 'KM' => __( 'Comoros', 'woocommerce' ),
88 'CG' => __( 'Congo (Brazzaville)', 'woocommerce' ),
89 'CD' => __( 'Congo (Kinshasa)', 'woocommerce' ),
90 'CK' => __( 'Cook Islands', 'woocommerce' ),
91 'CR' => __( 'Costa Rica', 'woocommerce' ),
92 'HR' => __( 'Croatia', 'woocommerce' ),
93 'CU' => __( 'Cuba', 'woocommerce' ),
94 'CW' => __( 'CuraÇao', 'woocommerce' ),
95 'CY' => __( 'Cyprus', 'woocommerce' ),
96 'CZ' => __( 'Czech Republic', 'woocommerce' ),
97 'DK' => __( 'Denmark', 'woocommerce' ),
98 'DJ' => __( 'Djibouti', 'woocommerce' ),
99 'DM' => __( 'Dominica', 'woocommerce' ),
100 'DO' => __( 'Dominican Republic', 'woocommerce' ),
101 'EC' => __( 'Ecuador', 'woocommerce' ),
102 'EG' => __( 'Egypt', 'woocommerce' ),
103 'SV' => __( 'El Salvador', 'woocommerce' ),
104 'GQ' => __( 'Equatorial Guinea', 'woocommerce' ),
105 'ER' => __( 'Eritrea', 'woocommerce' ),
106 'EE' => __( 'Estonia', 'woocommerce' ),
107 'ET' => __( 'Ethiopia', 'woocommerce' ),
108 'FK' => __( 'Falkland Islands', 'woocommerce' ),
109 'FO' => __( 'Faroe Islands', 'woocommerce' ),
110 'FJ' => __( 'Fiji', 'woocommerce' ),
111 'FI' => __( 'Finland', 'woocommerce' ),
112 'FR' => __( 'France', 'woocommerce' ),
113 'GF' => __( 'French Guiana', 'woocommerce' ),
114 'PF' => __( 'French Polynesia', 'woocommerce' ),
115 'TF' => __( 'French Southern Territories', 'woocommerce' ),
116 'GA' => __( 'Gabon', 'woocommerce' ),
117 'GM' => __( 'Gambia', 'woocommerce' ),
118 'GE' => __( 'Georgia', 'woocommerce' ),
119 'DE' => __( 'Germany', 'woocommerce' ),
120 'GH' => __( 'Ghana', 'woocommerce' ),
121 'GI' => __( 'Gibraltar', 'woocommerce' ),
122 'GR' => __( 'Greece', 'woocommerce' ),
123 'GL' => __( 'Greenland', 'woocommerce' ),
124 'GD' => __( 'Grenada', 'woocommerce' ),
125 'GP' => __( 'Guadeloupe', 'woocommerce' ),
126 'GT' => __( 'Guatemala', 'woocommerce' ),
127 'GG' => __( 'Guernsey', 'woocommerce' ),
128 'GN' => __( 'Guinea', 'woocommerce' ),
129 'GW' => __( 'Guinea-Bissau', 'woocommerce' ),
130 'GY' => __( 'Guyana', 'woocommerce' ),
131 'HT' => __( 'Haiti', 'woocommerce' ),
132 'HM' => __( 'Heard Island and McDonald Islands', 'woocommerce' ),
133 'HN' => __( 'Honduras', 'woocommerce' ),
134 'HK' => __( 'Hong Kong', 'woocommerce' ),
135 'HU' => __( 'Hungary', 'woocommerce' ),
136 'IS' => __( 'Iceland', 'woocommerce' ),
137 'IN' => __( 'India', 'woocommerce' ),
138 'ID' => __( 'Indonesia', 'woocommerce' ),
139 'IR' => __( 'Iran', 'woocommerce' ),
140 'IQ' => __( 'Iraq', 'woocommerce' ),
141 'IE' => __( 'Republic of Ireland', 'woocommerce' ),
142 'IM' => __( 'Isle of Man', 'woocommerce' ),
143 'IL' => __( 'Israel', 'woocommerce' ),
144 'IT' => __( 'Italy', 'woocommerce' ),
145 'CI' => __( 'Ivory Coast', 'woocommerce' ),
146 'JM' => __( 'Jamaica', 'woocommerce' ),
147 'JP' => __( 'Japan', 'woocommerce' ),
148 'JE' => __( 'Jersey', 'woocommerce' ),
149 'JO' => __( 'Jordan', 'woocommerce' ),
150 'KZ' => __( 'Kazakhstan', 'woocommerce' ),
151 'KE' => __( 'Kenya', 'woocommerce' ),
152 'KI' => __( 'Kiribati', 'woocommerce' ),
153 'KW' => __( 'Kuwait', 'woocommerce' ),
154 'KG' => __( 'Kyrgyzstan', 'woocommerce' ),
155 'LA' => __( 'Laos', 'woocommerce' ),
156 'LV' => __( 'Latvia', 'woocommerce' ),
157 'LB' => __( 'Lebanon', 'woocommerce' ),
158 'LS' => __( 'Lesotho', 'woocommerce' ),
159 'LR' => __( 'Liberia', 'woocommerce' ),
160 'LY' => __( 'Libya', 'woocommerce' ),
161 'LI' => __( 'Liechtenstein', 'woocommerce' ),
162 'LT' => __( 'Lithuania', 'woocommerce' ),
163 'LU' => __( 'Luxembourg', 'woocommerce' ),
164 'MO' => __( 'Macao S.A.R., China', 'woocommerce' ),
165 'MK' => __( 'Macedonia', 'woocommerce' ),
166 'MG' => __( 'Madagascar', 'woocommerce' ),
167 'MW' => __( 'Malawi', 'woocommerce' ),
168 'MY' => __( 'Malaysia', 'woocommerce' ),
169 'MV' => __( 'Maldives', 'woocommerce' ),
170 'ML' => __( 'Mali', 'woocommerce' ),
171 'MT' => __( 'Malta', 'woocommerce' ),
172 'MH' => __( 'Marshall Islands', 'woocommerce' ),
173 'MQ' => __( 'Martinique', 'woocommerce' ),
174 'MR' => __( 'Mauritania', 'woocommerce' ),
175 'MU' => __( 'Mauritius', 'woocommerce' ),
176 'YT' => __( 'Mayotte', 'woocommerce' ),
177 'MX' => __( 'Mexico', 'woocommerce' ),
178 'FM' => __( 'Micronesia', 'woocommerce' ),
179 'MD' => __( 'Moldova', 'woocommerce' ),
180 'MC' => __( 'Monaco', 'woocommerce' ),
181 'MN' => __( 'Mongolia', 'woocommerce' ),
182 'ME' => __( 'Montenegro', 'woocommerce' ),
183 'MS' => __( 'Montserrat', 'woocommerce' ),
184 'MA' => __( 'Morocco', 'woocommerce' ),
185 'MZ' => __( 'Mozambique', 'woocommerce' ),
186 'MM' => __( 'Myanmar', 'woocommerce' ),
187 'NA' => __( 'Namibia', 'woocommerce' ),
188 'NR' => __( 'Nauru', 'woocommerce' ),
189 'NP' => __( 'Nepal', 'woocommerce' ),
190 'NL' => __( 'Netherlands', 'woocommerce' ),
191 'AN' => __( 'Netherlands Antilles', 'woocommerce' ),
192 'NC' => __( 'New Caledonia', 'woocommerce' ),
193 'NZ' => __( 'New Zealand', 'woocommerce' ),
194 'NI' => __( 'Nicaragua', 'woocommerce' ),
195 'NE' => __( 'Niger', 'woocommerce' ),
196 'NG' => __( 'Nigeria', 'woocommerce' ),
197 'NU' => __( 'Niue', 'woocommerce' ),
198 'NF' => __( 'Norfolk Island', 'woocommerce' ),
199 'KP' => __( 'North Korea', 'woocommerce' ),
200 'NO' => __( 'Norway', 'woocommerce' ),
201 'OM' => __( 'Oman', 'woocommerce' ),
202 'PK' => __( 'Pakistan', 'woocommerce' ),
203 'PS' => __( 'Palestinian Territory', 'woocommerce' ),
204 'PA' => __( 'Panama', 'woocommerce' ),
205 'PG' => __( 'Papua New Guinea', 'woocommerce' ),
206 'PY' => __( 'Paraguay', 'woocommerce' ),
207 'PE' => __( 'Peru', 'woocommerce' ),
208 'PH' => __( 'Philippines', 'woocommerce' ),
209 'PN' => __( 'Pitcairn', 'woocommerce' ),
210 'PL' => __( 'Poland', 'woocommerce' ),
211 'PT' => __( 'Portugal', 'woocommerce' ),
212 'QA' => __( 'Qatar', 'woocommerce' ),
213 'RE' => __( 'Reunion', 'woocommerce' ),
214 'RO' => __( 'Romania', 'woocommerce' ),
215 'RU' => __( 'Russia', 'woocommerce' ),
216 'RW' => __( 'Rwanda', 'woocommerce' ),
217 'BL' => __( 'Saint Barthélemy', 'woocommerce' ),
218 'SH' => __( 'Saint Helena', 'woocommerce' ),
219 'KN' => __( 'Saint Kitts and Nevis', 'woocommerce' ),
220 'LC' => __( 'Saint Lucia', 'woocommerce' ),
221 'MF' => __( 'Saint Martin (French part)', 'woocommerce' ),
222 'SX' => __( 'Saint Martin (Dutch part)', 'woocommerce' ),
223 'PM' => __( 'Saint Pierre and Miquelon', 'woocommerce' ),
224 'VC' => __( 'Saint Vincent and the Grenadines', 'woocommerce' ),
225 'SM' => __( 'San Marino', 'woocommerce' ),
226 'ST' => __( 'São Tomé and Príncipe', 'woocommerce' ),
227 'SA' => __( 'Saudi Arabia', 'woocommerce' ),
228 'SN' => __( 'Senegal', 'woocommerce' ),
229 'RS' => __( 'Serbia', 'woocommerce' ),
230 'SC' => __( 'Seychelles', 'woocommerce' ),
231 'SL' => __( 'Sierra Leone', 'woocommerce' ),
232 'SG' => __( 'Singapore', 'woocommerce' ),
233 'SK' => __( 'Slovakia', 'woocommerce' ),
234 'SI' => __( 'Slovenia', 'woocommerce' ),
235 'SB' => __( 'Solomon Islands', 'woocommerce' ),
236 'SO' => __( 'Somalia', 'woocommerce' ),
237 'ZA' => __( 'South Africa', 'woocommerce' ),
238 'GS' => __( 'South Georgia/Sandwich Islands', 'woocommerce' ),
239 'KR' => __( 'South Korea', 'woocommerce' ),
240 'SS' => __( 'South Sudan', 'woocommerce' ),
241 'ES' => __( 'Spain', 'woocommerce' ),
242 'LK' => __( 'Sri Lanka', 'woocommerce' ),
243 'SD' => __( 'Sudan', 'woocommerce' ),
244 'SR' => __( 'Suriname', 'woocommerce' ),
245 'SJ' => __( 'Svalbard and Jan Mayen', 'woocommerce' ),
246 'SZ' => __( 'Swaziland', 'woocommerce' ),
247 'SE' => __( 'Sweden', 'woocommerce' ),
248 'CH' => __( 'Switzerland', 'woocommerce' ),
249 'SY' => __( 'Syria', 'woocommerce' ),
250 'TW' => __( 'Taiwan', 'woocommerce' ),
251 'TJ' => __( 'Tajikistan', 'woocommerce' ),
252 'TZ' => __( 'Tanzania', 'woocommerce' ),
253 'TH' => __( 'Thailand', 'woocommerce' ),
254 'TL' => __( 'Timor-Leste', 'woocommerce' ),
255 'TG' => __( 'Togo', 'woocommerce' ),
256 'TK' => __( 'Tokelau', 'woocommerce' ),
257 'TO' => __( 'Tonga', 'woocommerce' ),
258 'TT' => __( 'Trinidad and Tobago', 'woocommerce' ),
259 'TN' => __( 'Tunisia', 'woocommerce' ),
260 'TR' => __( 'Turkey', 'woocommerce' ),
261 'TM' => __( 'Turkmenistan', 'woocommerce' ),
262 'TC' => __( 'Turks and Caicos Islands', 'woocommerce' ),
263 'TV' => __( 'Tuvalu', 'woocommerce' ),
264 'UG' => __( 'Uganda', 'woocommerce' ),
265 'UA' => __( 'Ukraine', 'woocommerce' ),
266 'AE' => __( 'United Arab Emirates', 'woocommerce' ),
267 'GB' => __( 'United Kingdom (UK)', 'woocommerce' ),
268 'US' => __( 'United States (US)', 'woocommerce' ),
269 'UY' => __( 'Uruguay', 'woocommerce' ),
270 'UZ' => __( 'Uzbekistan', 'woocommerce' ),
271 'VU' => __( 'Vanuatu', 'woocommerce' ),
272 'VA' => __( 'Vatican', 'woocommerce' ),
273 'VE' => __( 'Venezuela', 'woocommerce' ),
274 'VN' => __( 'Vietnam', 'woocommerce' ),
275 'WF' => __( 'Wallis and Futuna', 'woocommerce' ),
276 'EH' => __( 'Western Sahara', 'woocommerce' ),
277 'WS' => __( 'Western Samoa', 'woocommerce' ),
278 'YE' => __( 'Yemen', 'woocommerce' ),
279 'ZM' => __( 'Zambia', 'woocommerce' ),
280 'ZW' => __( 'Zimbabwe', 'woocommerce' )
281 ));
282
283
284 $states = array(
285 'AF' => array(),
286 'AT' => array(),
287 'BE' => array(),
288 'BI' => array(),
289 'CZ' => array(),
290 'DE' => array(),
291 'DK' => array(),
292 'EE' => array(),
293 'FI' => array(),
294 'FR' => array(),
295 'IS' => array(),
296 'IL' => array(),
297 'KR' => array(),
298 'NL' => array(),
299 'NO' => array(),
300 'PL' => array(),
301 'PT' => array(),
302 'SG' => array(),
303 'SK' => array(),
304 'SI' => array(),
305 'LK' => array(),
306 'SE' => array(),
307 'VN' => array(),
308 );
309
310
311 $allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() );
312
313 if ( $allowed )
314 foreach ( $allowed as $CC => $country )
315 if ( ! isset( $states[ $CC ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $CC . '.php' ) )
316 include( WC()->plugin_path() . '/i18n/states/' . $CC . '.php' );
317
318 $this->states = apply_filters( 'woocommerce_states', $states );
319 }
320
321 322 323 324 325 326
327 public function get_base_country() {
328 $default = esc_attr( get_option('woocommerce_default_country') );
329 $country = ( ( $pos = strrpos( $default, ':' ) ) === false ) ? $default : substr( $default, 0, $pos );
330
331 return apply_filters( 'woocommerce_countries_base_country', $country );
332 }
333
334 335 336 337 338 339
340 public function get_base_state() {
341 $default = wc_clean( get_option( 'woocommerce_default_country' ) );
342 $state = ( ( $pos = strrpos( $default, ':' ) ) === false ) ? '' : substr( $default, $pos + 1 );
343
344 return apply_filters( 'woocommerce_countries_base_state', $state );
345 }
346
347 348 349 350 351 352
353 public function get_base_city() {
354 return apply_filters( 'woocommerce_countries_base_city', '' );
355 }
356
357 358 359 360 361 362
363 public function get_base_postcode() {
364 return apply_filters( 'woocommerce_countries_base_postcode', '' );
365 }
366
367 368 369 370 371 372
373 public function get_allowed_countries() {
374
375 if ( apply_filters('woocommerce_sort_countries', true ) )
376 asort( $this->countries );
377
378 if ( get_option('woocommerce_allowed_countries') !== 'specific' )
379 return $this->countries;
380
381 $countries = array();
382
383 $raw_countries = get_option( 'woocommerce_specific_allowed_countries' );
384
385 foreach ( $raw_countries as $country )
386 $countries[ $country ] = $this->countries[ $country ];
387
388 return apply_filters( 'woocommerce_countries_allowed_countries', $countries );
389 }
390
391 392 393 394 395 396
397 public function get_shipping_countries() {
398
399 if ( apply_filters( 'woocommerce_sort_countries', true ) )
400 asort( $this->countries );
401
402 if ( get_option( 'woocommerce_ship_to_countries' ) == '' )
403 return $this->get_allowed_countries();
404
405 if ( get_option('woocommerce_ship_to_countries') !== 'specific' )
406 return $this->countries;
407
408 $countries = array();
409
410 $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
411
412 foreach ( $raw_countries as $country )
413 $countries[ $country ] = $this->countries[ $country ];
414
415 return apply_filters( 'woocommerce_countries_shipping_countries', $countries );
416 }
417
418 419 420 421 422 423
424 public function get_allowed_country_states() {
425
426 if ( get_option('woocommerce_allowed_countries') !== 'specific' )
427 return $this->states;
428
429 $states = array();
430
431 $raw_countries = get_option( 'woocommerce_specific_allowed_countries' );
432
433 foreach ( $raw_countries as $country )
434 if ( isset( $this->states[ $country ] ) )
435 $states[ $country ] = $this->states[ $country ];
436
437 return apply_filters( 'woocommerce_countries_allowed_country_states', $states );
438 }
439
440 441 442 443 444 445
446 public function get_shipping_country_states() {
447
448 if ( get_option( 'woocommerce_ship_to_countries' ) == '' )
449 return $this->get_allowed_country_states();
450
451 if ( get_option( 'woocommerce_ship_to_countries' ) !== 'specific' )
452 return $this->states;
453
454 $states = array();
455
456 $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
457
458 foreach ( $raw_countries as $country )
459 if ( ! empty( $this->states[ $country ] ) )
460 $states[ $country ] = $this->states[ $country ];
461
462 return apply_filters( 'woocommerce_countries_shipping_country_states', $states );
463 }
464
465 466 467 468 469 470
471 public function get_european_union_countries() {
472 return array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU', 'HR', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );
473 }
474
475
476 477 478 479 480 481
482 public function shipping_to_prefix() {
483 $return = '';
484 if (in_array(WC()->customer->get_shipping_country(), array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' ))) $return = __( 'to the', 'woocommerce' );
485 else $return = __( 'to', 'woocommerce' );
486 return apply_filters('woocommerce_countries_shipping_to_prefix', $return, WC()->customer->get_shipping_country());
487 }
488
489
490 491 492 493 494 495
496 public function estimated_for_prefix() {
497 $return = '';
498 if (in_array($this->get_base_country(), array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' ))) $return = __( 'the', 'woocommerce' ) . ' ';
499 return apply_filters('woocommerce_countries_estimated_for_prefix', $return, $this->get_base_country());
500 }
501
502
503 504 505 506 507 508
509 public function tax_or_vat() {
510 $return = ( in_array($this->get_base_country(), $this->get_european_union_countries()) ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
511
512 return apply_filters( 'woocommerce_countries_tax_or_vat', $return );
513 }
514
515
516 517 518 519 520 521
522 public function inc_tax_or_vat() {
523 $return = ( in_array($this->get_base_country(), $this->get_european_union_countries()) ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' );
524
525 return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return );
526 }
527
528
529 530 531 532 533 534
535 public function ex_tax_or_vat() {
536 $return = ( in_array($this->get_base_country(), $this->get_european_union_countries()) ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' );
537
538 return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return );
539 }
540
541
542 543 544 545 546 547 548
549 public function get_states( $cc ) {
550 return ( isset( $this->states[ $cc ] ) ) ? $this->states[ $cc ] : false;
551 }
552
553
554 555 556 557 558 559 560 561 562
563 public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
564
565 if ( apply_filters('woocommerce_sort_countries', true ) )
566 asort( $this->countries );
567
568 if ( $this->countries ) foreach ( $this->countries as $key=>$value) :
569 if ( $states = $this->get_states($key) ) :
570 echo '<optgroup label="' . esc_attr( $value ) . '">';
571 foreach ($states as $state_key=>$state_value) :
572 echo '<option value="' . esc_attr( $key ) . ':'.$state_key.'"';
573
574 if ($selected_country==$key && $selected_state==$state_key) echo ' selected="selected"';
575
576 echo '>'.$value.' — '. ($escape ? esc_js($state_value) : $state_value) .'</option>';
577 endforeach;
578 echo '</optgroup>';
579 else :
580 echo '<option';
581 if ($selected_country==$key && $selected_state=='*') echo ' selected="selected"';
582 echo ' value="' . esc_attr( $key ) . '">'. ($escape ? esc_js( $value ) : $value) .'</option>';
583 endif;
584 endforeach;
585 }
586
587
588 589 590 591 592 593
594 public function get_address_formats() {
595
596 if (!$this->address_formats) :
597
598
599 $postcode_before_city = "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}";
600
601
602 $this->address_formats = apply_filters('woocommerce_localisation_address_formats', array(
603 'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
604 'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
605 'AT' => $postcode_before_city,
606 'BE' => $postcode_before_city,
607 'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
608 'CH' => $postcode_before_city,
609 'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}",
610 'CZ' => $postcode_before_city,
611 'DE' => $postcode_before_city,
612 'EE' => $postcode_before_city,
613 'FI' => $postcode_before_city,
614 'DK' => $postcode_before_city,
615 'FR' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",
616 'HK' => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",
617 'HU' => "{name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",
618 'IS' => $postcode_before_city,
619 'IT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",
620 'JP' => "{postcode}\n{state}{city}{address_2}\n{address_1}\n{company}\n{last_name} {first_name}\n {country}",
621 'LI' => $postcode_before_city,
622 'NL' => $postcode_before_city,
623 'NZ' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}",
624 'NO' => $postcode_before_city,
625 'PL' => $postcode_before_city,
626 'SK' => $postcode_before_city,
627 'SI' => $postcode_before_city,
628 'ES' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",
629 'SE' => $postcode_before_city,
630 'TR' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}",
631 'US' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state} {postcode}\n{country}",
632 'VN' => "{name}\n{company}\n{address_1}\n{city}\n{country}",
633 ));
634 endif;
635
636 return $this->address_formats;
637 }
638
639
640 641 642 643 644 645 646
647 public function get_formatted_address( $args = array() ) {
648
649 $args = array_map( 'trim', $args );
650
651 extract( $args );
652
653
654 $formats = $this->get_address_formats();
655
656
657 $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
658
659
660 $full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;
661
662
663 if ( $country == $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) )
664 $format = str_replace( '{country}', '', $format );
665
666
667 $full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;
668
669
670 $replace = array_map( 'esc_html', apply_filters( 'woocommerce_formatted_address_replacements', array(
671 '{first_name}' => $first_name,
672 '{last_name}' => $last_name,
673 '{name}' => $first_name . ' ' . $last_name,
674 '{company}' => $company,
675 '{address_1}' => $address_1,
676 '{address_2}' => $address_2,
677 '{city}' => $city,
678 '{state}' => $full_state,
679 '{postcode}' => $postcode,
680 '{country}' => $full_country,
681 '{first_name_upper}' => strtoupper( $first_name ),
682 '{last_name_upper}' => strtoupper( $last_name ),
683 '{name_upper}' => strtoupper( $first_name . ' ' . $last_name ),
684 '{company_upper}' => strtoupper( $company ),
685 '{address_1_upper}' => strtoupper( $address_1 ),
686 '{address_2_upper}' => strtoupper( $address_2 ),
687 '{city_upper}' => strtoupper( $city ),
688 '{state_upper}' => strtoupper( $full_state ),
689 '{postcode_upper}' => strtoupper( $postcode ),
690 '{country_upper}' => strtoupper( $full_country ),
691 ), $args ) );
692
693 $formatted_address = str_replace( array_keys( $replace ), $replace, $format );
694
695
696 $formatted_address = preg_replace( '/ +/', ' ', trim( $formatted_address ) );
697 $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
698
699
700 $formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );
701
702
703 $formatted_address = implode( '<br/>', $formatted_address );
704
705
706 return $formatted_address;
707 }
708
709 710 711 712 713
714 private function trim_formatted_address_line( $line ) {
715 return trim( $line, ", " );
716 }
717
718
719 720 721 722 723 724
725 public function get_default_address_fields() {
726 $fields = array(
727 'country' => array(
728 'type' => 'country',
729 'label' => __( 'Country', 'woocommerce' ),
730 'required' => true,
731 'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
732 ),
733 'first_name' => array(
734 'label' => __( 'First Name', 'woocommerce' ),
735 'required' => true,
736 'class' => array( 'form-row-first' ),
737 ),
738 'last_name' => array(
739 'label' => __( 'Last Name', 'woocommerce' ),
740 'required' => true,
741 'class' => array( 'form-row-last' ),
742 'clear' => true
743 ),
744 'company' => array(
745 'label' => __( 'Company Name', 'woocommerce' ),
746 'class' => array( 'form-row-wide' ),
747 ),
748 'address_1' => array(
749 'label' => __( 'Address', 'woocommerce' ),
750 'placeholder' => _x( 'Street address', 'placeholder', 'woocommerce' ),
751 'required' => true,
752 'class' => array( 'form-row-wide', 'address-field' )
753 ),
754 'address_2' => array(
755 'placeholder' => _x( 'Apartment, suite, unit etc. (optional)', 'placeholder', 'woocommerce' ),
756 'class' => array( 'form-row-wide', 'address-field' ),
757 'required' => false
758 ),
759 'city' => array(
760 'label' => __( 'Town / City', 'woocommerce' ),
761 'placeholder' => __( 'Town / City', 'woocommerce' ),
762 'required' => true,
763 'class' => array( 'form-row-wide', 'address-field' )
764 ),
765 'state' => array(
766 'type' => 'state',
767 'label' => __( 'State / County', 'woocommerce' ),
768 'placeholder' => __( 'State / County', 'woocommerce' ),
769 'required' => true,
770 'class' => array( 'form-row-first', 'address-field' ),
771 'validate' => array( 'state' )
772 ),
773 'postcode' => array(
774 'label' => __( 'Postcode / Zip', 'woocommerce' ),
775 'placeholder' => __( 'Postcode / Zip', 'woocommerce' ),
776 'required' => true,
777 'class' => array( 'form-row-last', 'address-field' ),
778 'clear' => true,
779 'validate' => array( 'postcode' )
780 ),
781 );
782
783 return apply_filters( 'woocommerce_default_address_fields', $fields );
784 }
785
786 787 788 789 790 791
792 public function get_country_locale_field_selectors() {
793 $locale_fields = array (
794 'address_1' => '#billing_address_1_field, #shipping_address_1_field',
795 'address_2' => '#billing_address_2_field, #shipping_address_2_field',
796 'state' => '#billing_state_field, #shipping_state_field',
797 'postcode' => '#billing_postcode_field, #shipping_postcode_field',
798 'city' => '#billing_city_field, #shipping_city_field'
799 );
800
801 return apply_filters( 'woocommerce_country_locale_field_selectors', $locale_fields );
802 }
803
804 805 806 807 808 809
810 public function get_country_locale() {
811 if ( ! $this->locale ) {
812
813
814 $this->locale = apply_filters('woocommerce_get_country_locale', array(
815 'AE' => array(
816 'postcode' => array(
817 'required' => false,
818 'hidden' => true
819 ),
820 ),
821 'AF' => array(
822 'state' => array(
823 'required' => false,
824 ),
825 ),
826 'AT' => array(
827 'postcode_before_city' => true,
828 'state' => array(
829 'required' => false
830 )
831 ),
832 'BD' => array(
833 'postcode' => array(
834 'required' => false
835 ),
836 'state' => array(
837 'label' => __( 'District', 'woocommerce' ),
838 )
839 ),
840 'BE' => array(
841 'postcode_before_city' => true,
842 'state' => array(
843 'required' => false,
844 'label' => __( 'Province', 'woocommerce' ),
845 ),
846 ),
847 'BI' => array(
848 'state' => array(
849 'required' => false,
850 ),
851 ),
852 'BO' => array(
853 'postcode' => array(
854 'required' => false,
855 'hidden' => true
856 ),
857 ),
858 'CA' => array(
859 'state' => array(
860 'label' => __( 'Province', 'woocommerce' ),
861 )
862 ),
863 'CH' => array(
864 'postcode_before_city' => true,
865 'state' => array(
866 'label' => __( 'Canton', 'woocommerce' ),
867 'required' => false
868 )
869 ),
870 'CL' => array(
871 'city' => array(
872 'required' => false,
873 ),
874 'state' => array(
875 'label' => __( 'Municipality', 'woocommerce' ),
876 )
877 ),
878 'CN' => array(
879 'state' => array(
880 'label' => __( 'Province', 'woocommerce' ),
881 )
882 ),
883 'CO' => array(
884 'postcode' => array(
885 'required' => false
886 )
887 ),
888 'CZ' => array(
889 'state' => array(
890 'required' => false
891 )
892 ),
893 'DE' => array(
894 'postcode_before_city' => true,
895 'state' => array(
896 'required' => false
897 )
898 ),
899 'DK' => array(
900 'postcode_before_city' => true,
901 'state' => array(
902 'required' => false
903 )
904 ),
905 'EE' => array(
906 'postcode_before_city' => true,
907 'state' => array(
908 'required' => false
909 )
910 ),
911 'FI' => array(
912 'postcode_before_city' => true,
913 'state' => array(
914 'required' => false
915 )
916 ),
917 'FR' => array(
918 'postcode_before_city' => true,
919 'state' => array(
920 'required' => false
921 )
922 ),
923 'HK' => array(
924 'postcode' => array(
925 'required' => false
926 ),
927 'city' => array(
928 'label' => __( 'Town / District', 'woocommerce' ),
929 ),
930 'state' => array(
931 'label' => __( 'Region', 'woocommerce' ),
932 )
933 ),
934 'HU' => array(
935 'state' => array(
936 'label' => __( 'County', 'woocommerce' ),
937 )
938 ),
939 'ID' => array(
940 'state' => array(
941 'label' => __( 'Province', 'woocommerce' ),
942 )
943 ),
944 'IS' => array(
945 'postcode_before_city' => true,
946 'state' => array(
947 'required' => false
948 )
949 ),
950 'IL' => array(
951 'postcode_before_city' => true,
952 'state' => array(
953 'required' => false
954 )
955 ),
956 'IT' => array(
957 'postcode_before_city' => true,
958 'state' => array(
959 'required' => true,
960 'label' => __( 'Province', 'woocommerce' ),
961 )
962 ),
963 'JP' => array(
964 'state' => array(
965 'label' => __( 'Province', 'woocommerce' )
966 )
967 ),
968 'KR' => array(
969 'state' => array(
970 'required' => false
971 )
972 ),
973 'NL' => array(
974 'postcode_before_city' => true,
975 'state' => array(
976 'required' => false,
977 'label' => __( 'Province', 'woocommerce' ),
978 )
979 ),
980 'NZ' => array(
981 'state' => array(
982 'required' => false
983 )
984 ),
985 'NO' => array(
986 'postcode_before_city' => true,
987 'state' => array(
988 'required' => false
989 )
990 ),
991 'PL' => array(
992 'postcode_before_city' => true,
993 'state' => array(
994 'required' => false
995 )
996 ),
997 'PT' => array(
998 'state' => array(
999 'required' => false
1000 )
1001 ),
1002 'RO' => array(
1003 'state' => array(
1004 'required' => false
1005 )
1006 ),
1007 'SG' => array(
1008 'state' => array(
1009 'required' => false
1010 )
1011 ),
1012 'SK' => array(
1013 'postcode_before_city' => true,
1014 'state' => array(
1015 'required' => false
1016 )
1017 ),
1018 'SI' => array(
1019 'postcode_before_city' => true,
1020 'state' => array(
1021 'required' => false
1022 )
1023 ),
1024 'ES' => array(
1025 'postcode_before_city' => true,
1026 'state' => array(
1027 'label' => __( 'Province', 'woocommerce' ),
1028 )
1029 ),
1030 'LI' => array(
1031 'postcode_before_city' => true,
1032 'state' => array(
1033 'label' => __( 'Municipality', 'woocommerce' ),
1034 'required' => false
1035 )
1036 ),
1037 'LK' => array(
1038 'state' => array(
1039 'required' => false
1040 )
1041 ),
1042 'SE' => array(
1043 'postcode_before_city' => true,
1044 'state' => array(
1045 'required' => false
1046 )
1047 ),
1048 'TR' => array(
1049 'postcode_before_city' => true,
1050 'state' => array(
1051 'label' => __( 'Province', 'woocommerce' ),
1052 )
1053 ),
1054 'US' => array(
1055 'postcode' => array(
1056 'label' => __( 'Zip', 'woocommerce' ),
1057 ),
1058 'state' => array(
1059 'label' => __( 'State', 'woocommerce' ),
1060 )
1061 ),
1062 'GB' => array(
1063 'postcode' => array(
1064 'label' => __( 'Postcode', 'woocommerce' ),
1065 ),
1066 'state' => array(
1067 'label' => __( 'County', 'woocommerce' ),
1068 'required' => false
1069 )
1070 ),
1071 'VN' => array(
1072 'state' => array(
1073 'required' => false
1074 ),
1075 'postcode' => array(
1076 'required' => false,
1077 'hidden' => true
1078 ),
1079 'address_2' => array(
1080 'required' => false,
1081 'hidden' => true
1082 )
1083 ),
1084 'WS' => array(
1085 'postcode' => array(
1086 'required' => false,
1087 'hidden' => true
1088 ),
1089 ),
1090 'ZA' => array(
1091 'state' => array(
1092 'label' => __( 'Province', 'woocommerce' ),
1093 )
1094 ),
1095 'ZW' => array(
1096 'postcode' => array(
1097 'required' => false,
1098 'hidden' => true
1099 ),
1100 ),
1101 ));
1102
1103 $this->locale = array_intersect_key( $this->locale, array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ) );
1104
1105
1106
1107 $this->locale['default'] = apply_filters('woocommerce_get_country_locale_default', $this->get_default_address_fields() );
1108
1109
1110 if ( ! isset( $this->locale[ $this->get_base_country() ] ) )
1111 $this->locale[ $this->get_base_country() ] = $this->locale['default'];
1112
1113 $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale['default'] );
1114 $this->locale[ $this->get_base_country() ] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale[ $this->get_base_country() ] );
1115 }
1116
1117 return $this->locale;
1118 }
1119
1120 1121 1122 1123 1124 1125 1126 1127
1128 public function get_address_fields( $country, $type = 'billing_' ) {
1129
1130 if (!$country)
1131 $country = $this->get_base_country();
1132
1133 $fields = $this->get_default_address_fields();
1134 $locale = $this->get_country_locale();
1135
1136 if ( isset( $locale[ $country ] ) ) {
1137
1138 $fields = wc_array_overlay( $fields, $locale[ $country ] );
1139
1140
1141
1142 if ( isset( $locale[ $country ]['postcode_before_city'] ) ) {
1143 if ( isset( $fields['postcode'] ) ) {
1144 $fields['postcode']['class'] = array( 'form-row-wide', 'address-field' );
1145
1146 $switch_fields = array();
1147
1148 foreach ( $fields as $key => $value ) {
1149 if ( $key == 'city' ) {
1150
1151 $switch_fields['postcode'] = '';
1152 }
1153 $switch_fields[$key] = $value;
1154 }
1155
1156 $fields = $switch_fields;
1157 }
1158 }
1159 }
1160
1161
1162 $address_fields = array();
1163
1164 foreach ( $fields as $key => $value ) {
1165 $address_fields[$type . $key] = $value;
1166 }
1167
1168
1169 if ( $type == 'billing_' ) {
1170
1171 $address_fields['billing_email'] = array(
1172 'label' => __( 'Email Address', 'woocommerce' ),
1173 'required' => true,
1174 'class' => array( 'form-row-first' ),
1175 'validate' => array( 'email' ),
1176 );
1177 $address_fields['billing_phone'] = array(
1178 'label' => __( 'Phone', 'woocommerce' ),
1179 'required' => true,
1180 'class' => array( 'form-row-last' ),
1181 'clear' => true,
1182 'validate' => array( 'phone' ),
1183 );
1184
1185 }
1186
1187 $address_fields = apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );
1188
1189
1190 return $address_fields;
1191 }
1192 }
1193