WC_Countries

WooCommerce countries.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-countries.php  
  1. class WC_Countries { 
  2.  
  3. /** @var array Array of locales */ 
  4. public $locale; 
  5.  
  6. /** @var array Array of address formats for locales */ 
  7. public $address_formats; 
  8.  
  9. /** 
  10. * Auto-load in-accessible properties on demand. 
  11. * @param mixed $key 
  12. * @return mixed 
  13. */ 
  14. public function __get( $key ) { 
  15. if ( 'countries' == $key ) { 
  16. return $this->get_countries(); 
  17. } elseif ( 'states' == $key ) { 
  18. return $this->get_states(); 
  19.  
  20. /** 
  21. * Get all countries. 
  22. * @return array 
  23. */ 
  24. public function get_countries() { 
  25. if ( empty( $this->countries ) ) { 
  26. $this->countries = apply_filters( 'woocommerce_countries', include( WC()->plugin_path() . '/i18n/countries.php' ) ); 
  27. if ( apply_filters( 'woocommerce_sort_countries', true ) ) { 
  28. asort( $this->countries ); 
  29. return $this->countries; 
  30.  
  31. /** 
  32. * Get all continents. 
  33. * @return array 
  34. */ 
  35. public function get_continents() { 
  36. if ( empty( $this->continents ) ) { 
  37. $this->continents = apply_filters( 'woocommerce_continents', include( WC()->plugin_path() . '/i18n/continents.php' ) ); 
  38. return $this->continents; 
  39.  
  40. /** 
  41. * Get continent code for a country code. 
  42. * @since 2.6.0 
  43. * @param string $cc string 
  44. * @return string 
  45. */ 
  46. public function get_continent_code_for_country( $cc ) { 
  47. $cc = trim( strtoupper( $cc ) ); 
  48. $continents = $this->get_continents(); 
  49. $continents_and_ccs = wp_list_pluck( $continents, 'countries' ); 
  50. foreach ( $continents_and_ccs as $continent_code => $countries ) { 
  51. if ( false !== array_search( $cc, $countries ) ) { 
  52. return $continent_code; 
  53. return ''; 
  54.  
  55. /** 
  56. * Load the states. 
  57. */ 
  58. public function load_country_states() { 
  59. global $states; 
  60.  
  61. // States set to array() are blank i.e. the country has no use for the state field. 
  62. $states = array( 
  63. 'AF' => array(),  
  64. 'AT' => array(),  
  65. 'AX' => array(),  
  66. 'BE' => array(),  
  67. 'BI' => array(),  
  68. 'CZ' => array(),  
  69. 'DE' => array(),  
  70. 'DK' => array(),  
  71. 'EE' => array(),  
  72. 'FI' => array(),  
  73. 'FR' => array(),  
  74. 'GP' => array(),  
  75. 'GF' => array(),  
  76. 'IS' => array(),  
  77. 'IL' => array(),  
  78. 'KR' => array(),  
  79. 'KW' => array(),  
  80. 'LB' => array(),  
  81. 'MQ' => array(),  
  82. 'NL' => array(),  
  83. 'NO' => array(),  
  84. 'PL' => array(),  
  85. 'PT' => array(),  
  86. 'RE' => array(),  
  87. 'SG' => array(),  
  88. 'SK' => array(),  
  89. 'SI' => array(),  
  90. 'LK' => array(),  
  91. 'SE' => array(),  
  92. 'VN' => array(),  
  93. 'YT' => array(),  
  94. ); 
  95.  
  96. // Load only the state files the shop owner wants/needs. 
  97. $allowed = array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ); 
  98.  
  99. if ( ! empty( $allowed ) ) { 
  100. foreach ( $allowed as $code => $country ) { 
  101. if ( ! isset( $states[ $code ] ) && file_exists( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ) ) { 
  102. include( WC()->plugin_path() . '/i18n/states/' . $code . '.php' ); 
  103.  
  104. $this->states = apply_filters( 'woocommerce_states', $states ); 
  105.  
  106. /** 
  107. * Get the states for a country. 
  108. * @param string $cc country code 
  109. * @return array of states 
  110. */ 
  111. public function get_states( $cc = null ) { 
  112. if ( empty( $this->states ) ) { 
  113. $this->load_country_states(); 
  114.  
  115. if ( ! is_null( $cc ) ) { 
  116. return isset( $this->states[ $cc ] ) ? $this->states[ $cc ] : false; 
  117. } else { 
  118. return $this->states; 
  119.  
  120. /** 
  121. * Get the base country for the store. 
  122. * @return string 
  123. */ 
  124. public function get_base_country() { 
  125. $default = wc_get_base_location(); 
  126. return apply_filters( 'woocommerce_countries_base_country', $default['country'] ); 
  127.  
  128. /** 
  129. * Get the base state for the store. 
  130. * @return string 
  131. */ 
  132. public function get_base_state() { 
  133. $default = wc_get_base_location(); 
  134. return apply_filters( 'woocommerce_countries_base_state', $default['state'] ); 
  135.  
  136. /** 
  137. * Get the base city for the store. 
  138. * @return string 
  139. */ 
  140. public function get_base_city() { 
  141. return apply_filters( 'woocommerce_countries_base_city', '' ); 
  142.  
  143. /** 
  144. * Get the base postcode for the store. 
  145. * @return string 
  146. */ 
  147. public function get_base_postcode() { 
  148. return apply_filters( 'woocommerce_countries_base_postcode', '' ); 
  149.  
  150. /** 
  151. * Get the allowed countries for the store. 
  152. * @return array 
  153. */ 
  154. public function get_allowed_countries() { 
  155. if ( 'all' === get_option( 'woocommerce_allowed_countries' ) ) { 
  156. return $this->countries; 
  157.  
  158. if ( 'all_except' === get_option( 'woocommerce_allowed_countries' ) ) { 
  159. $except_countries = get_option( 'woocommerce_all_except_countries', array() ); 
  160.  
  161. if ( ! $except_countries ) { 
  162. return $this->countries; 
  163. } else { 
  164. $all_except_countries = $this->countries; 
  165. foreach ( $except_countries as $country ) { 
  166. unset( $all_except_countries[ $country ] ); 
  167. return apply_filters( 'woocommerce_countries_allowed_countries', $all_except_countries ); 
  168.  
  169. $countries = array(); 
  170.  
  171. $raw_countries = get_option( 'woocommerce_specific_allowed_countries', array() ); 
  172.  
  173. if ( $raw_countries ) { 
  174. foreach ( $raw_countries as $country ) { 
  175. $countries[ $country ] = $this->countries[ $country ]; 
  176.  
  177. return apply_filters( 'woocommerce_countries_allowed_countries', $countries ); 
  178.  
  179. /** 
  180. * Get the countries you ship to. 
  181. * @return array 
  182. */ 
  183. public function get_shipping_countries() { 
  184. if ( '' === get_option( 'woocommerce_ship_to_countries' ) ) { 
  185. return $this->get_allowed_countries(); 
  186.  
  187. if ( 'all' === get_option( 'woocommerce_ship_to_countries' ) ) { 
  188. return $this->countries; 
  189.  
  190. $countries = array(); 
  191.  
  192. $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' ); 
  193.  
  194. if ( $raw_countries ) { 
  195. foreach ( $raw_countries as $country ) { 
  196. $countries[ $country ] = $this->countries[ $country ]; 
  197.  
  198. return apply_filters( 'woocommerce_countries_shipping_countries', $countries ); 
  199.  
  200. /** 
  201. * Get allowed country states. 
  202. * @return array 
  203. */ 
  204. public function get_allowed_country_states() { 
  205. if ( get_option( 'woocommerce_allowed_countries' ) !== 'specific' ) { 
  206. return $this->states; 
  207.  
  208. $states = array(); 
  209.  
  210. $raw_countries = get_option( 'woocommerce_specific_allowed_countries' ); 
  211.  
  212. if ( $raw_countries ) { 
  213. foreach ( $raw_countries as $country ) { 
  214. if ( isset( $this->states[ $country ] ) ) { 
  215. $states[ $country ] = $this->states[ $country ]; 
  216.  
  217. return apply_filters( 'woocommerce_countries_allowed_country_states', $states ); 
  218.  
  219. /** 
  220. * Get shipping country states. 
  221. * @return array 
  222. */ 
  223. public function get_shipping_country_states() { 
  224. if ( get_option( 'woocommerce_ship_to_countries' ) == '' ) { 
  225. return $this->get_allowed_country_states(); 
  226.  
  227. if ( get_option( 'woocommerce_ship_to_countries' ) !== 'specific' ) { 
  228. return $this->states; 
  229.  
  230. $states = array(); 
  231.  
  232. $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' ); 
  233.  
  234. if ( $raw_countries ) { 
  235. foreach ( $raw_countries as $country ) { 
  236. if ( ! empty( $this->states[ $country ] ) ) { 
  237. $states[ $country ] = $this->states[ $country ]; 
  238.  
  239. return apply_filters( 'woocommerce_countries_shipping_country_states', $states ); 
  240.  
  241. /** 
  242. * Gets an array of countries in the EU. 
  243. * MC (monaco) and IM (isle of man, part of UK) also use VAT. 
  244. * @param $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries. 
  245. * @return string[] 
  246. */ 
  247. public function get_european_union_countries( $type = '' ) { 
  248. $countries = 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' ); 
  249.  
  250. if ( 'eu_vat' === $type ) { 
  251. $countries[] = 'MC'; 
  252. $countries[] = 'IM'; 
  253.  
  254. return $countries; 
  255.  
  256. /** 
  257. * Gets the correct string for shipping - either 'to the' or 'to' 
  258. * @return string 
  259. */ 
  260. public function shipping_to_prefix( $country_code = '' ) { 
  261. $country_code = $country_code ? $country_code : WC()->customer->get_shipping_country(); 
  262. $countries = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' ); 
  263. $return = in_array( $country_code, $countries ) ? __( 'to the', 'woocommerce' ) : __( 'to', 'woocommerce' ); 
  264.  
  265. return apply_filters( 'woocommerce_countries_shipping_to_prefix', $return, $country_code ); 
  266.  
  267. /** 
  268. * Prefix certain countries with 'the' 
  269. * @return string 
  270. */ 
  271. public function estimated_for_prefix( $country_code = '' ) { 
  272. $country_code = $country_code ? $country_code : $this->get_base_country(); 
  273. $countries = array( 'GB', 'US', 'AE', 'CZ', 'DO', 'NL', 'PH', 'USAF' ); 
  274. $return = in_array( $country_code, $countries ) ? __( 'the', 'woocommerce' ) . ' ' : ''; 
  275.  
  276. return apply_filters( 'woocommerce_countries_estimated_for_prefix', $return, $country_code ); 
  277.  
  278. /** 
  279. * Correctly name tax in some countries VAT on the frontend. 
  280. * @return string 
  281. */ 
  282. public function tax_or_vat() { 
  283. $return = in_array( $this->get_base_country(), $this->get_european_union_countries( 'eu_vat' ) ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' ); 
  284.  
  285. return apply_filters( 'woocommerce_countries_tax_or_vat', $return ); 
  286.  
  287. /** 
  288. * Include the Inc Tax label. 
  289. * @return string 
  290. */ 
  291. public function inc_tax_or_vat() { 
  292. $return = in_array( $this->get_base_country(), $this->get_european_union_countries( 'eu_vat' ) ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' ); 
  293.  
  294. return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return ); 
  295.  
  296. /** 
  297. * Include the Ex Tax label. 
  298. * @return string 
  299. */ 
  300. public function ex_tax_or_vat() { 
  301. $return = in_array( $this->get_base_country(), $this->get_european_union_countries( 'eu_vat' ) ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' ); 
  302.  
  303. return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return ); 
  304.  
  305. /** 
  306. * Outputs the list of countries and states for use in dropdown boxes. 
  307. * @param string $selected_country (default: '') 
  308. * @param string $selected_state (default: '') 
  309. * @param bool $escape (default: false) 
  310. * @param bool $escape (default: false) 
  311. */ 
  312. public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) { 
  313. if ( $this->countries ) foreach ( $this->countries as $key => $value ) : 
  314. if ( $states = $this->get_states( $key ) ) : 
  315. echo '<optgroup label="' . esc_attr( $value ) . '">'; 
  316. foreach ( $states as $state_key => $state_value ) : 
  317. echo '<option value="' . esc_attr( $key ) . ':' . $state_key . '"'; 
  318.  
  319. if ( $selected_country == $key && $selected_state == $state_key ) { 
  320. echo ' selected="selected"'; 
  321.  
  322. echo '>' . $value . ' — ' . ( $escape ? esc_js( $state_value ) : $state_value ) . '</option>'; 
  323. endforeach; 
  324. echo '</optgroup>'; 
  325. else : 
  326. echo '<option'; 
  327. if ( $selected_country == $key && '*' == $selected_state ) { 
  328. echo ' selected="selected"'; 
  329. echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_js( $value ) : $value ) . '</option>'; 
  330. endif; 
  331. endforeach; 
  332.  
  333. /** 
  334. * Get country address formats. 
  335. * These define how addresses are formatted for display in various countries. 
  336. * @return array 
  337. */ 
  338. public function get_address_formats() { 
  339. if ( empty( $this->address_formats ) ) { 
  340. $this->address_formats = apply_filters( 'woocommerce_localisation_address_formats', array( 
  341. 'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",  
  342. 'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",  
  343. 'AT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  344. 'BE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  345. 'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",  
  346. 'CH' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  347. 'CL' => "{company}\n{name}\n{address_1}\n{address_2}\n{state}\n{postcode} {city}\n{country}",  
  348. 'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}",  
  349. 'CZ' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  350. 'DE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  351. 'EE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  352. 'FI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  353. 'DK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  354. 'FR' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",  
  355. 'HK' => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",  
  356. 'HU' => "{name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",  
  357. 'IN' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} - {postcode}\n{state}, {country}",  
  358. 'IS' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  359. 'IT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",  
  360. 'JP' => "{postcode}\n{state}{city}{address_1}\n{address_2}\n{company}\n{last_name} {first_name}\n{country}",  
  361. 'TW' => "{company}\n{last_name} {first_name}\n{address_1}\n{address_2}\n{state}, {city} {postcode}\n{country}",  
  362. 'LI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  363. 'NL' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  364. 'NZ' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}",  
  365. 'NO' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  366. 'PL' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  367. 'PT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  368. 'SK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  369. 'SI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  370. 'ES' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",  
  371. 'SE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",  
  372. 'TR' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}",  
  373. 'US' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state_code} {postcode}\n{country}",  
  374. 'VN' => "{name}\n{company}\n{address_1}\n{city}\n{country}",  
  375. ) ); 
  376. return $this->address_formats; 
  377.  
  378. /** 
  379. * Get country address format. 
  380. * @param array $args (default: array()) 
  381. * @return string address 
  382. */ 
  383. public function get_formatted_address( $args = array() ) { 
  384. $default_args = array( 
  385. 'first_name' => '',  
  386. 'last_name' => '',  
  387. 'company' => '',  
  388. 'address_1' => '',  
  389. 'address_2' => '',  
  390. 'city' => '',  
  391. 'state' => '',  
  392. 'postcode' => '',  
  393. 'country' => '',  
  394. ); 
  395.  
  396. $args = array_map( 'trim', wp_parse_args( $args, $default_args ) ); 
  397.  
  398. extract( $args ); 
  399.  
  400. // Get all formats 
  401. $formats = $this->get_address_formats(); 
  402.  
  403. // Get format for the address' country 
  404. $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; 
  405.  
  406. // Handle full country name 
  407. $full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country; 
  408.  
  409. // Country is not needed if the same as base 
  410. if ( $country == $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) ) { 
  411. $format = str_replace( '{country}', '', $format ); 
  412.  
  413. // Handle full state name 
  414. $full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state; 
  415.  
  416. // Substitute address parts into the string 
  417. $replace = array_map( 'esc_html', apply_filters( 'woocommerce_formatted_address_replacements', array( 
  418. '{first_name}' => $first_name,  
  419. '{last_name}' => $last_name,  
  420. '{name}' => $first_name . ' ' . $last_name,  
  421. '{company}' => $company,  
  422. '{address_1}' => $address_1,  
  423. '{address_2}' => $address_2,  
  424. '{city}' => $city,  
  425. '{state}' => $full_state,  
  426. '{postcode}' => $postcode,  
  427. '{country}' => $full_country,  
  428. '{first_name_upper}' => strtoupper( $first_name ),  
  429. '{last_name_upper}' => strtoupper( $last_name ),  
  430. '{name_upper}' => strtoupper( $first_name . ' ' . $last_name ),  
  431. '{company_upper}' => strtoupper( $company ),  
  432. '{address_1_upper}' => strtoupper( $address_1 ),  
  433. '{address_2_upper}' => strtoupper( $address_2 ),  
  434. '{city_upper}' => strtoupper( $city ),  
  435. '{state_upper}' => strtoupper( $full_state ),  
  436. '{state_code}' => strtoupper( $state ),  
  437. '{postcode_upper}' => strtoupper( $postcode ),  
  438. '{country_upper}' => strtoupper( $full_country ),  
  439. ), $args ) ); 
  440.  
  441. $formatted_address = str_replace( array_keys( $replace ), $replace, $format ); 
  442.  
  443. // Clean up white space 
  444. $formatted_address = preg_replace( '/ +/', ' ', trim( $formatted_address ) ); 
  445. $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address ); 
  446.  
  447. // Break newlines apart and remove empty lines/trim commas and white space 
  448. $formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) ); 
  449.  
  450. // Add html breaks 
  451. $formatted_address = implode( '<br/>', $formatted_address ); 
  452.  
  453. // We're done! 
  454. return $formatted_address; 
  455.  
  456. /** 
  457. * Trim white space and commas off a line. 
  458. * @param string $line 
  459. * @return string 
  460. */ 
  461. private function trim_formatted_address_line( $line ) { 
  462. return trim( $line, ", " ); 
  463.  
  464. /** 
  465. * Returns the fields we show by default. This can be filtered later on. 
  466. * @return array 
  467. */ 
  468. public function get_default_address_fields() { 
  469. $fields = array( 
  470. 'first_name' => array( 
  471. 'label' => __( 'First name', 'woocommerce' ),  
  472. 'required' => true,  
  473. 'class' => array( 'form-row-first' ),  
  474. 'autocomplete' => 'given-name',  
  475. 'autofocus' => true,  
  476. 'priority' => 10,  
  477. ),  
  478. 'last_name' => array( 
  479. 'label' => __( 'Last name', 'woocommerce' ),  
  480. 'required' => true,  
  481. 'class' => array( 'form-row-last' ),  
  482. 'autocomplete' => 'family-name',  
  483. 'priority' => 20,  
  484. ),  
  485. 'company' => array( 
  486. 'label' => __( 'Company name', 'woocommerce' ),  
  487. 'class' => array( 'form-row-wide' ),  
  488. 'autocomplete' => 'organization',  
  489. 'priority' => 30,  
  490. ),  
  491. 'country' => array( 
  492. 'type' => 'country',  
  493. 'label' => __( 'Country', 'woocommerce' ),  
  494. 'required' => true,  
  495. 'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),  
  496. 'autocomplete' => 'country',  
  497. 'priority' => 40,  
  498. ),  
  499. 'address_1' => array( 
  500. 'label' => __( 'Address', 'woocommerce' ),  
  501. 'placeholder' => esc_attr__( 'Street address', 'woocommerce' ),  
  502. 'required' => true,  
  503. 'class' => array( 'form-row-wide', 'address-field' ),  
  504. 'autocomplete' => 'address-line1',  
  505. 'priority' => 50,  
  506. ),  
  507. 'address_2' => array( 
  508. 'placeholder' => esc_attr__( 'Apartment, suite, unit etc. (optional)', 'woocommerce' ),  
  509. 'class' => array( 'form-row-wide', 'address-field' ),  
  510. 'required' => false,  
  511. 'autocomplete' => 'address-line2',  
  512. 'priority' => 60,  
  513. ),  
  514. 'city' => array( 
  515. 'label' => __( 'Town / City', 'woocommerce' ),  
  516. 'required' => true,  
  517. 'class' => array( 'form-row-wide', 'address-field' ),  
  518. 'autocomplete' => 'address-level2',  
  519. 'priority' => 70,  
  520. ),  
  521. 'state' => array( 
  522. 'type' => 'state',  
  523. 'label' => __( 'State / County', 'woocommerce' ),  
  524. 'required' => true,  
  525. 'class' => array( 'form-row-wide', 'address-field' ),  
  526. 'validate' => array( 'state' ),  
  527. 'autocomplete' => 'address-level1',  
  528. 'priority' => 80,  
  529. ),  
  530. 'postcode' => array( 
  531. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),  
  532. 'required' => true,  
  533. 'class' => array( 'form-row-wide', 'address-field' ),  
  534. 'validate' => array( 'postcode' ),  
  535. 'autocomplete' => 'postal-code',  
  536. 'priority' => 90,  
  537. ),  
  538. ); 
  539.  
  540. return apply_filters( 'woocommerce_default_address_fields', $fields ); 
  541.  
  542. /** 
  543. * Get JS selectors for fields which are shown/hidden depending on the locale. 
  544. * @return array 
  545. */ 
  546. public function get_country_locale_field_selectors() { 
  547. $locale_fields = array( 
  548. 'address_1' => '#billing_address_1_field, #shipping_address_1_field',  
  549. 'address_2' => '#billing_address_2_field, #shipping_address_2_field',  
  550. 'state' => '#billing_state_field, #shipping_state_field, #calc_shipping_state_field',  
  551. 'postcode' => '#billing_postcode_field, #shipping_postcode_field, #calc_shipping_postcode_field',  
  552. 'city' => '#billing_city_field, #shipping_city_field, #calc_shipping_city_field',  
  553. ); 
  554. return apply_filters( 'woocommerce_country_locale_field_selectors', $locale_fields ); 
  555.  
  556. /** 
  557. * Get country locale settings. 
  558. * These locales override the default country selections after a country is chosen. 
  559. * @return array 
  560. */ 
  561. public function get_country_locale() { 
  562. if ( empty( $this->locale ) ) { 
  563. $this->locale = apply_filters( 'woocommerce_get_country_locale', array( 
  564. 'AE' => array( 
  565. 'postcode' => array( 
  566. 'required' => false,  
  567. 'hidden' => true,  
  568. ),  
  569. 'state' => array( 
  570. 'required' => false,  
  571. ),  
  572. ),  
  573. 'AF' => array( 
  574. 'state' => array( 
  575. 'required' => false,  
  576. ),  
  577. ),  
  578. 'AT' => array( 
  579. 'postcode' => array( 
  580. 'priority' => 65,  
  581. ),  
  582. 'state' => array( 
  583. 'required' => false,  
  584. ),  
  585. ),  
  586. 'AU' => array( 
  587. 'city' => array( 
  588. 'label' => __( 'Suburb', 'woocommerce' ),  
  589. ),  
  590. 'postcode' => array( 
  591. 'label' => __( 'Postcode', 'woocommerce' ),  
  592. ),  
  593. 'state' => array( 
  594. 'label' => __( 'State', 'woocommerce' ),  
  595. ),  
  596. ),  
  597. 'AX' => array( 
  598. 'postcode' => array( 
  599. 'priority' => 65,  
  600. ),  
  601. 'state' => array( 
  602. 'required' => false,  
  603. ),  
  604. ),  
  605. 'BD' => array( 
  606. 'postcode' => array( 
  607. 'required' => false,  
  608. ),  
  609. 'state' => array( 
  610. 'label' => __( 'District', 'woocommerce' ),  
  611. ),  
  612. ),  
  613. 'BE' => array( 
  614. 'postcode' => array( 
  615. 'priority' => 65,  
  616. ),  
  617. 'state' => array( 
  618. 'required' => false,  
  619. 'label' => __( 'Province', 'woocommerce' ),  
  620. ),  
  621. ),  
  622. 'BI' => array( 
  623. 'state' => array( 
  624. 'required' => false,  
  625. ),  
  626. ),  
  627. 'BO' => array( 
  628. 'postcode' => array( 
  629. 'required' => false,  
  630. 'hidden' => true,  
  631. ),  
  632. ),  
  633. 'BS' => array( 
  634. 'postcode' => array( 
  635. 'required' => false,  
  636. 'hidden' => true,  
  637. ),  
  638. ),  
  639. 'CA' => array( 
  640. 'state' => array( 
  641. 'label' => __( 'Province', 'woocommerce' ),  
  642. ),  
  643. ),  
  644. 'CH' => array( 
  645. 'postcode' => array( 
  646. 'priority' => 65,  
  647. ),  
  648. 'state' => array( 
  649. 'label' => __( 'Canton', 'woocommerce' ),  
  650. 'required' => false,  
  651. ),  
  652. ),  
  653. 'CL' => array( 
  654. 'city' => array( 
  655. 'required' => true,  
  656. ),  
  657. 'postcode' => array( 
  658. 'required' => false,  
  659. ),  
  660. 'state' => array( 
  661. 'label' => __( 'Region', 'woocommerce' ),  
  662. ),  
  663. ),  
  664. 'CN' => array( 
  665. 'state' => array( 
  666. 'label' => __( 'Province', 'woocommerce' ),  
  667. ),  
  668. ),  
  669. 'CO' => array( 
  670. 'postcode' => array( 
  671. 'required' => false,  
  672. ),  
  673. ),  
  674. 'CZ' => array( 
  675. 'state' => array( 
  676. 'required' => false,  
  677. ),  
  678. ),  
  679. 'DE' => array( 
  680. 'postcode' => array( 
  681. 'priority' => 65,  
  682. ),  
  683. 'state' => array( 
  684. 'required' => false,  
  685. ),  
  686. ),  
  687. 'DK' => array( 
  688. 'postcode' => array( 
  689. 'priority' => 65,  
  690. ),  
  691. 'state' => array( 
  692. 'required' => false,  
  693. ),  
  694. ),  
  695. 'EE' => array( 
  696. 'postcode' => array( 
  697. 'priority' => 65,  
  698. ),  
  699. 'state' => array( 
  700. 'required' => false,  
  701. ),  
  702. ),  
  703. 'FI' => array( 
  704. 'postcode' => array( 
  705. 'priority' => 65,  
  706. ),  
  707. 'state' => array( 
  708. 'required' => false,  
  709. ),  
  710. ),  
  711. 'FR' => array( 
  712. 'postcode' => array( 
  713. 'priority' => 65,  
  714. ),  
  715. 'state' => array( 
  716. 'required' => false,  
  717. ),  
  718. ),  
  719. 'GP' => array( 
  720. 'state' => array( 
  721. 'required' => false,  
  722. ),  
  723. ),  
  724. 'GF' => array( 
  725. 'state' => array( 
  726. 'required' => false,  
  727. ),  
  728. ),  
  729. 'HK' => array( 
  730. 'postcode' => array( 
  731. 'required' => false,  
  732. ),  
  733. 'city' => array( 
  734. 'label' => __( 'Town / District', 'woocommerce' ),  
  735. ),  
  736. 'state' => array( 
  737. 'label' => __( 'Region', 'woocommerce' ),  
  738. ),  
  739. ),  
  740. 'HU' => array( 
  741. 'state' => array( 
  742. 'label' => __( 'County', 'woocommerce' ),  
  743. ),  
  744. ),  
  745. 'ID' => array( 
  746. 'state' => array( 
  747. 'label' => __( 'Province', 'woocommerce' ),  
  748. ),  
  749. ),  
  750. 'IE' => array( 
  751. 'postcode' => array( 
  752. 'required' => false,  
  753. 'label' => __( 'Eircode', 'woocommerce' ),  
  754. ),  
  755. 'state' => array( 
  756. 'label' => __( 'County', 'woocommerce' ),  
  757. ),  
  758. ),  
  759. 'IS' => array( 
  760. 'postcode' => array( 
  761. 'priority' => 65,  
  762. ),  
  763. 'state' => array( 
  764. 'required' => false,  
  765. ),  
  766. ),  
  767. 'IL' => array( 
  768. 'postcode' => array( 
  769. 'priority' => 65,  
  770. ),  
  771. 'state' => array( 
  772. 'required' => false,  
  773. ),  
  774. ),  
  775. 'IT' => array( 
  776. 'postcode' => array( 
  777. 'priority' => 65,  
  778. ),  
  779. 'state' => array( 
  780. 'required' => true,  
  781. 'label' => __( 'Province', 'woocommerce' ),  
  782. ),  
  783. ),  
  784. 'JP' => array( 
  785. 'state' => array( 
  786. 'label' => __( 'Prefecture', 'woocommerce' ),  
  787. 'priority' => 66,  
  788. ),  
  789. 'postcode' => array( 
  790. 'priority' => 65,  
  791. ),  
  792. ),  
  793. 'KR' => array( 
  794. 'state' => array( 
  795. 'required' => false,  
  796. ),  
  797. ),  
  798. 'KW' => array( 
  799. 'state' => array( 
  800. 'required' => false,  
  801. ),  
  802. ),  
  803. 'LB' => array( 
  804. 'state' => array( 
  805. 'required' => false,  
  806. ),  
  807. ),  
  808. 'MQ' => array( 
  809. 'state' => array( 
  810. 'required' => false,  
  811. ),  
  812. ),  
  813. 'NL' => array( 
  814. 'postcode' => array( 
  815. 'priority' => 65,  
  816. ),  
  817. 'state' => array( 
  818. 'required' => false,  
  819. 'label' => __( 'Province', 'woocommerce' ),  
  820. ),  
  821. ),  
  822. 'NZ' => array( 
  823. 'postcode' => array( 
  824. 'label' => __( 'Postcode', 'woocommerce' ),  
  825. ),  
  826. 'state' => array( 
  827. 'required' => false,  
  828. 'label' => __( 'Region', 'woocommerce' ),  
  829. ),  
  830. ),  
  831. 'NO' => array( 
  832. 'postcode' => array( 
  833. 'priority' => 65,  
  834. ),  
  835. 'state' => array( 
  836. 'required' => false,  
  837. ),  
  838. ),  
  839. 'NP' => array( 
  840. 'state' => array( 
  841. 'label' => __( 'State / Zone', 'woocommerce' ),  
  842. ),  
  843. 'postcode' => array( 
  844. 'required' => false,  
  845. ),  
  846. ),  
  847. 'PL' => array( 
  848. 'postcode' => array( 
  849. 'priority' => 65,  
  850. ),  
  851. 'state' => array( 
  852. 'required' => false,  
  853. ),  
  854. ),  
  855. 'PT' => array( 
  856. 'state' => array( 
  857. 'required' => false,  
  858. ),  
  859. ),  
  860. 'RE' => array( 
  861. 'state' => array( 
  862. 'required' => false,  
  863. ),  
  864. ),  
  865. 'RO' => array( 
  866. 'state' => array( 
  867. 'required' => false,  
  868. ),  
  869. ),  
  870. 'SG' => array( 
  871. 'state' => array( 
  872. 'required' => false,  
  873. ),  
  874. ),  
  875. 'SK' => array( 
  876. 'postcode' => array( 
  877. 'priority' => 65,  
  878. ),  
  879. 'state' => array( 
  880. 'required' => false,  
  881. ),  
  882. ),  
  883. 'SI' => array( 
  884. 'postcode' => array( 
  885. 'priority' => 65,  
  886. ),  
  887. 'state' => array( 
  888. 'required' => false,  
  889. ),  
  890. ),  
  891. 'ES' => array( 
  892. 'postcode' => array( 
  893. 'priority' => 65,  
  894. ),  
  895. 'state' => array( 
  896. 'label' => __( 'Province', 'woocommerce' ),  
  897. ),  
  898. ),  
  899. 'LI' => array( 
  900. 'postcode' => array( 
  901. 'priority' => 65,  
  902. ),  
  903. 'state' => array( 
  904. 'label' => __( 'Municipality', 'woocommerce' ),  
  905. 'required' => false,  
  906. ),  
  907. ),  
  908. 'LK' => array( 
  909. 'state' => array( 
  910. 'required' => false,  
  911. ),  
  912. ),  
  913. 'SE' => array( 
  914. 'postcode' => array( 
  915. 'priority' => 65,  
  916. ),  
  917. 'state' => array( 
  918. 'required' => false,  
  919. ),  
  920. ),  
  921. 'TR' => array( 
  922. 'postcode' => array( 
  923. 'priority' => 65,  
  924. ),  
  925. 'state' => array( 
  926. 'label' => __( 'Province', 'woocommerce' ),  
  927. ),  
  928. ),  
  929. 'US' => array( 
  930. 'postcode' => array( 
  931. 'label' => __( 'ZIP', 'woocommerce' ),  
  932. ),  
  933. 'state' => array( 
  934. 'label' => __( 'State', 'woocommerce' ),  
  935. ),  
  936. ),  
  937. 'GB' => array( 
  938. 'postcode' => array( 
  939. 'label' => __( 'Postcode', 'woocommerce' ),  
  940. ),  
  941. 'state' => array( 
  942. 'label' => __( 'County', 'woocommerce' ),  
  943. 'required' => false,  
  944. ),  
  945. ),  
  946. 'VN' => array( 
  947. 'state' => array( 
  948. 'required' => false,  
  949. ),  
  950. 'postcode' => array( 
  951. 'priority' => 65,  
  952. 'required' => false,  
  953. 'hidden' => false,  
  954. ),  
  955. 'address_2' => array( 
  956. 'required' => false,  
  957. 'hidden' => true,  
  958. ),  
  959. ),  
  960. 'WS' => array( 
  961. 'postcode' => array( 
  962. 'required' => false,  
  963. 'hidden' => true,  
  964. ),  
  965. ),  
  966. 'YT' => array( 
  967. 'state' => array( 
  968. 'required' => false,  
  969. ),  
  970. ),  
  971. 'ZA' => array( 
  972. 'state' => array( 
  973. 'label' => __( 'Province', 'woocommerce' ),  
  974. ),  
  975. ),  
  976. 'ZW' => array( 
  977. 'postcode' => array( 
  978. 'required' => false,  
  979. 'hidden' => true,  
  980. ),  
  981. ),  
  982. )); 
  983.  
  984. $this->locale = array_intersect_key( $this->locale, array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ) ); 
  985.  
  986. // Default Locale Can be filtered to override fields in get_address_fields(). Countries with no specific locale will use default. 
  987. $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_default', $this->get_default_address_fields() ); 
  988.  
  989. // Filter default AND shop base locales to allow overides via a single function. These will be used when changing countries on the checkout 
  990. if ( ! isset( $this->locale[ $this->get_base_country() ] ) ) { 
  991. $this->locale[ $this->get_base_country() ] = $this->locale['default']; 
  992.  
  993. $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale['default'] ); 
  994. $this->locale[ $this->get_base_country() ] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale[ $this->get_base_country() ] ); 
  995.  
  996. return $this->locale; 
  997.  
  998. /** 
  999. * Apply locale and get address fields. 
  1000. * @param mixed $country (default: '') 
  1001. * @param string $type (default: 'billing_') 
  1002. * @return array 
  1003. */ 
  1004. public function get_address_fields( $country = '', $type = 'billing_' ) { 
  1005. if ( ! $country ) { 
  1006. $country = $this->get_base_country(); 
  1007.  
  1008. $fields = $this->get_default_address_fields(); 
  1009. $locale = $this->get_country_locale(); 
  1010.  
  1011. if ( isset( $locale[ $country ] ) ) { 
  1012. $fields = wc_array_overlay( $fields, $locale[ $country ] ); 
  1013.  
  1014. // Prepend field keys 
  1015. $address_fields = array(); 
  1016.  
  1017. foreach ( $fields as $key => $value ) { 
  1018. $keys = array_keys( $fields ); 
  1019. $address_fields[ $type . $key ] = $value; 
  1020.  
  1021. // Add email and phone fields. 
  1022. if ( 'billing_' === $type ) { 
  1023. $address_fields['billing_phone'] = array( 
  1024. 'label' => __( 'Phone', 'woocommerce' ),  
  1025. 'required' => true,  
  1026. 'type' => 'tel',  
  1027. 'class' => array( 'form-row-first' ),  
  1028. 'validate' => array( 'phone' ),  
  1029. 'autocomplete' => 'tel',  
  1030. 'priority' => 100,  
  1031. ); 
  1032. $address_fields['billing_email'] = array( 
  1033. 'label' => __( 'Email address', 'woocommerce' ),  
  1034. 'required' => true,  
  1035. 'type' => 'email',  
  1036. 'class' => array( 'form-row-last' ),  
  1037. 'validate' => array( 'email' ),  
  1038. 'autocomplete' => 'no' === get_option( 'woocommerce_registration_generate_username' ) ? 'email' : 'email username',  
  1039. 'priority' => 110,  
  1040. ); 
  1041.  
  1042. /** 
  1043. * Important note on this filter: Changes to address fields can and will be overridden by 
  1044. * the woocommerce_default_address_fields. The locales/default locales apply on top based 
  1045. * on country selection. If you want to change things like the required status of an 
  1046. * address field, filter woocommerce_default_address_fields instead. 
  1047. */ 
  1048. return apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );