説明なし

class-wc-countries.php 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. <?php
  2. /**
  3. * WooCommerce countries
  4. *
  5. * @package WooCommerce\l10n
  6. * @version 3.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * The WooCommerce countries class stores country/state data.
  11. */
  12. class WC_Countries {
  13. /**
  14. * Locales list.
  15. *
  16. * @var array
  17. */
  18. public $locale = array();
  19. /**
  20. * List of address formats for locales.
  21. *
  22. * @var array
  23. */
  24. public $address_formats = array();
  25. /**
  26. * Auto-load in-accessible properties on demand.
  27. *
  28. * @param mixed $key Key.
  29. * @return mixed
  30. */
  31. public function __get( $key ) {
  32. if ( 'countries' === $key ) {
  33. return $this->get_countries();
  34. } elseif ( 'states' === $key ) {
  35. return $this->get_states();
  36. }
  37. }
  38. /**
  39. * Get all countries.
  40. *
  41. * @return array
  42. */
  43. public function get_countries() {
  44. if ( empty( $this->countries ) ) {
  45. $this->countries = apply_filters( 'woocommerce_countries', include WC()->plugin_path() . '/i18n/countries.php' );
  46. if ( apply_filters( 'woocommerce_sort_countries', true ) ) {
  47. wc_asort_by_locale( $this->countries );
  48. }
  49. }
  50. return $this->countries;
  51. }
  52. /**
  53. * Check if a given code represents a valid ISO 3166-1 alpha-2 code for a country known to us.
  54. *
  55. * @since 5.1.0
  56. * @param string $country_code The country code to check as a ISO 3166-1 alpha-2 code.
  57. * @return bool True if the country is known to us, false otherwise.
  58. */
  59. public function country_exists( $country_code ) {
  60. return isset( $this->get_countries()[ $country_code ] );
  61. }
  62. /**
  63. * Get all continents.
  64. *
  65. * @return array
  66. */
  67. public function get_continents() {
  68. if ( empty( $this->continents ) ) {
  69. $this->continents = apply_filters( 'woocommerce_continents', include WC()->plugin_path() . '/i18n/continents.php' );
  70. }
  71. return $this->continents;
  72. }
  73. /**
  74. * Get continent code for a country code.
  75. *
  76. * @since 2.6.0
  77. * @param string $cc Country code.
  78. * @return string
  79. */
  80. public function get_continent_code_for_country( $cc ) {
  81. $cc = trim( strtoupper( $cc ) );
  82. $continents = $this->get_continents();
  83. $continents_and_ccs = wp_list_pluck( $continents, 'countries' );
  84. foreach ( $continents_and_ccs as $continent_code => $countries ) {
  85. if ( false !== array_search( $cc, $countries, true ) ) {
  86. return $continent_code;
  87. }
  88. }
  89. return '';
  90. }
  91. /**
  92. * Get calling code for a country code.
  93. *
  94. * @since 3.6.0
  95. * @param string $cc Country code.
  96. * @return string|array Some countries have multiple. The code will be stripped of - and spaces and always be prefixed with +.
  97. */
  98. public function get_country_calling_code( $cc ) {
  99. $codes = wp_cache_get( 'calling-codes', 'countries' );
  100. if ( ! $codes ) {
  101. $codes = include WC()->plugin_path() . '/i18n/phone.php';
  102. wp_cache_set( 'calling-codes', $codes, 'countries' );
  103. }
  104. $calling_code = isset( $codes[ $cc ] ) ? $codes[ $cc ] : '';
  105. if ( is_array( $calling_code ) ) {
  106. $calling_code = $calling_code[0];
  107. }
  108. return $calling_code;
  109. }
  110. /**
  111. * Get continents that the store ships to.
  112. *
  113. * @since 3.6.0
  114. * @return array
  115. */
  116. public function get_shipping_continents() {
  117. $continents = $this->get_continents();
  118. $shipping_countries = $this->get_shipping_countries();
  119. $shipping_country_codes = array_keys( $shipping_countries );
  120. $shipping_continents = array();
  121. foreach ( $continents as $continent_code => $continent ) {
  122. if ( count( array_intersect( $continent['countries'], $shipping_country_codes ) ) ) {
  123. $shipping_continents[ $continent_code ] = $continent;
  124. }
  125. }
  126. return $shipping_continents;
  127. }
  128. /**
  129. * Load the states.
  130. *
  131. * @deprecated 3.6.0 This method was used to load state files, but is no longer needed. @see get_states().
  132. */
  133. public function load_country_states() {
  134. global $states;
  135. $states = include WC()->plugin_path() . '/i18n/states.php';
  136. $this->states = apply_filters( 'woocommerce_states', $states );
  137. }
  138. /**
  139. * Get the states for a country.
  140. *
  141. * @param string $cc Country code.
  142. * @return false|array of states
  143. */
  144. public function get_states( $cc = null ) {
  145. if ( ! isset( $this->states ) ) {
  146. $this->states = apply_filters( 'woocommerce_states', include WC()->plugin_path() . '/i18n/states.php' );
  147. }
  148. if ( ! is_null( $cc ) ) {
  149. return isset( $this->states[ $cc ] ) ? $this->states[ $cc ] : false;
  150. } else {
  151. return $this->states;
  152. }
  153. }
  154. /**
  155. * Get the base address (first line) for the store.
  156. *
  157. * @since 3.1.1
  158. * @return string
  159. */
  160. public function get_base_address() {
  161. $base_address = get_option( 'woocommerce_store_address', '' );
  162. return apply_filters( 'woocommerce_countries_base_address', $base_address );
  163. }
  164. /**
  165. * Get the base address (second line) for the store.
  166. *
  167. * @since 3.1.1
  168. * @return string
  169. */
  170. public function get_base_address_2() {
  171. $base_address_2 = get_option( 'woocommerce_store_address_2', '' );
  172. return apply_filters( 'woocommerce_countries_base_address_2', $base_address_2 );
  173. }
  174. /**
  175. * Get the base country for the store.
  176. *
  177. * @return string
  178. */
  179. public function get_base_country() {
  180. $default = wc_get_base_location();
  181. return apply_filters( 'woocommerce_countries_base_country', $default['country'] );
  182. }
  183. /**
  184. * Get the base state for the store.
  185. *
  186. * @return string
  187. */
  188. public function get_base_state() {
  189. $default = wc_get_base_location();
  190. return apply_filters( 'woocommerce_countries_base_state', $default['state'] );
  191. }
  192. /**
  193. * Get the base city for the store.
  194. *
  195. * @version 3.1.1
  196. * @return string
  197. */
  198. public function get_base_city() {
  199. $base_city = get_option( 'woocommerce_store_city', '' );
  200. return apply_filters( 'woocommerce_countries_base_city', $base_city );
  201. }
  202. /**
  203. * Get the base postcode for the store.
  204. *
  205. * @since 3.1.1
  206. * @return string
  207. */
  208. public function get_base_postcode() {
  209. $base_postcode = get_option( 'woocommerce_store_postcode', '' );
  210. return apply_filters( 'woocommerce_countries_base_postcode', $base_postcode );
  211. }
  212. /**
  213. * Get countries that the store sells to.
  214. *
  215. * @return array
  216. */
  217. public function get_allowed_countries() {
  218. if ( 'all' === get_option( 'woocommerce_allowed_countries' ) ) {
  219. return apply_filters( 'woocommerce_countries_allowed_countries', $this->countries );
  220. }
  221. if ( 'all_except' === get_option( 'woocommerce_allowed_countries' ) ) {
  222. $except_countries = get_option( 'woocommerce_all_except_countries', array() );
  223. if ( ! $except_countries ) {
  224. return $this->countries;
  225. } else {
  226. $all_except_countries = $this->countries;
  227. foreach ( $except_countries as $country ) {
  228. unset( $all_except_countries[ $country ] );
  229. }
  230. return apply_filters( 'woocommerce_countries_allowed_countries', $all_except_countries );
  231. }
  232. }
  233. $countries = array();
  234. $raw_countries = get_option( 'woocommerce_specific_allowed_countries', array() );
  235. if ( $raw_countries ) {
  236. foreach ( $raw_countries as $country ) {
  237. $countries[ $country ] = $this->countries[ $country ];
  238. }
  239. }
  240. return apply_filters( 'woocommerce_countries_allowed_countries', $countries );
  241. }
  242. /**
  243. * Get countries that the store ships to.
  244. *
  245. * @return array
  246. */
  247. public function get_shipping_countries() {
  248. if ( '' === get_option( 'woocommerce_ship_to_countries' ) ) {
  249. return $this->get_allowed_countries();
  250. }
  251. if ( 'all' === get_option( 'woocommerce_ship_to_countries' ) ) {
  252. return $this->countries;
  253. }
  254. $countries = array();
  255. $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
  256. if ( $raw_countries ) {
  257. foreach ( $raw_countries as $country ) {
  258. $countries[ $country ] = $this->countries[ $country ];
  259. }
  260. }
  261. return apply_filters( 'woocommerce_countries_shipping_countries', $countries );
  262. }
  263. /**
  264. * Get allowed country states.
  265. *
  266. * @return array
  267. */
  268. public function get_allowed_country_states() {
  269. if ( get_option( 'woocommerce_allowed_countries' ) !== 'specific' ) {
  270. return $this->states;
  271. }
  272. $states = array();
  273. $raw_countries = get_option( 'woocommerce_specific_allowed_countries' );
  274. if ( $raw_countries ) {
  275. foreach ( $raw_countries as $country ) {
  276. if ( isset( $this->states[ $country ] ) ) {
  277. $states[ $country ] = $this->states[ $country ];
  278. }
  279. }
  280. }
  281. return apply_filters( 'woocommerce_countries_allowed_country_states', $states );
  282. }
  283. /**
  284. * Get shipping country states.
  285. *
  286. * @return array
  287. */
  288. public function get_shipping_country_states() {
  289. if ( get_option( 'woocommerce_ship_to_countries' ) === '' ) {
  290. return $this->get_allowed_country_states();
  291. }
  292. if ( get_option( 'woocommerce_ship_to_countries' ) !== 'specific' ) {
  293. return $this->states;
  294. }
  295. $states = array();
  296. $raw_countries = get_option( 'woocommerce_specific_ship_to_countries' );
  297. if ( $raw_countries ) {
  298. foreach ( $raw_countries as $country ) {
  299. if ( ! empty( $this->states[ $country ] ) ) {
  300. $states[ $country ] = $this->states[ $country ];
  301. }
  302. }
  303. }
  304. return apply_filters( 'woocommerce_countries_shipping_country_states', $states );
  305. }
  306. /**
  307. * Gets an array of countries in the EU.
  308. *
  309. * @param string $type Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
  310. * @return string[]
  311. */
  312. public function get_european_union_countries( $type = '' ) {
  313. $countries = array( 'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GR', 'HR', 'HU', 'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK' );
  314. if ( 'eu_vat' === $type ) {
  315. $countries[] = 'MC';
  316. }
  317. return apply_filters( 'woocommerce_european_union_countries', $countries, $type );
  318. }
  319. /**
  320. * Gets an array of Non-EU countries that use VAT as the Local name for their taxes based on this list - https://en.wikipedia.org/wiki/Value-added_tax#Non-European_Union_countries
  321. *
  322. * @deprecated 4.0.0
  323. * @since 3.9.0
  324. * @return string[]
  325. */
  326. public function countries_using_vat() {
  327. wc_deprecated_function( 'countries_using_vat', '4.0', 'WC_Countries::get_vat_countries' );
  328. $countries = array( 'AE', 'AL', 'AR', 'AZ', 'BB', 'BH', 'BO', 'BS', 'BY', 'CL', 'CO', 'EC', 'EG', 'ET', 'FJ', 'GH', 'GM', 'GT', 'IL', 'IN', 'IR', 'KN', 'KR', 'KZ', 'LK', 'MD', 'ME', 'MK', 'MN', 'MU', 'MX', 'NA', 'NG', 'NP', 'PS', 'PY', 'RS', 'RU', 'RW', 'SA', 'SV', 'TH', 'TR', 'UA', 'UY', 'UZ', 'VE', 'VN', 'ZA' );
  329. return apply_filters( 'woocommerce_countries_using_vat', $countries );
  330. }
  331. /**
  332. * Gets an array of countries using VAT.
  333. *
  334. * @since 4.0.0
  335. * @return string[] of country codes.
  336. */
  337. public function get_vat_countries() {
  338. $eu_countries = $this->get_european_union_countries();
  339. $vat_countries = array( 'AE', 'AL', 'AR', 'AZ', 'BB', 'BH', 'BO', 'BS', 'BY', 'CL', 'CO', 'EC', 'EG', 'ET', 'FJ', 'GB', 'GH', 'GM', 'GT', 'IL', 'IM', 'IN', 'IR', 'KN', 'KR', 'KZ', 'LK', 'MC', 'MD', 'ME', 'MK', 'MN', 'MU', 'MX', 'NA', 'NG', 'NO', 'NP', 'PS', 'PY', 'RS', 'RU', 'RW', 'SA', 'SV', 'TH', 'TR', 'UA', 'UY', 'UZ', 'VE', 'VN', 'ZA' );
  340. return apply_filters( 'woocommerce_vat_countries', array_merge( $eu_countries, $vat_countries ) );
  341. }
  342. /**
  343. * Gets the correct string for shipping - either 'to the' or 'to'.
  344. *
  345. * @param string $country_code Country code.
  346. * @return string
  347. */
  348. public function shipping_to_prefix( $country_code = '' ) {
  349. $country_code = $country_code ? $country_code : WC()->customer->get_shipping_country();
  350. $countries = array( 'AE', 'CZ', 'DO', 'GB', 'NL', 'PH', 'US', 'USAF' );
  351. $return = in_array( $country_code, $countries, true ) ? __( 'to the', 'woocommerce' ) : __( 'to', 'woocommerce' );
  352. return apply_filters( 'woocommerce_countries_shipping_to_prefix', $return, $country_code );
  353. }
  354. /**
  355. * Prefix certain countries with 'the'.
  356. *
  357. * @param string $country_code Country code.
  358. * @return string
  359. */
  360. public function estimated_for_prefix( $country_code = '' ) {
  361. $country_code = $country_code ? $country_code : $this->get_base_country();
  362. $countries = array( 'AE', 'CZ', 'DO', 'GB', 'NL', 'PH', 'US', 'USAF' );
  363. $return = in_array( $country_code, $countries, true ) ? __( 'the', 'woocommerce' ) . ' ' : '';
  364. return apply_filters( 'woocommerce_countries_estimated_for_prefix', $return, $country_code );
  365. }
  366. /**
  367. * Correctly name tax in some countries VAT on the frontend.
  368. *
  369. * @return string
  370. */
  371. public function tax_or_vat() {
  372. $return = in_array( $this->get_base_country(), $this->get_vat_countries(), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
  373. return apply_filters( 'woocommerce_countries_tax_or_vat', $return );
  374. }
  375. /**
  376. * Include the Inc Tax label.
  377. *
  378. * @return string
  379. */
  380. public function inc_tax_or_vat() {
  381. $return = in_array( $this->get_base_country(), $this->get_vat_countries(), true ) ? __( '(incl. VAT)', 'woocommerce' ) : __( '(incl. tax)', 'woocommerce' );
  382. return apply_filters( 'woocommerce_countries_inc_tax_or_vat', $return );
  383. }
  384. /**
  385. * Include the Ex Tax label.
  386. *
  387. * @return string
  388. */
  389. public function ex_tax_or_vat() {
  390. $return = in_array( $this->get_base_country(), $this->get_vat_countries(), true ) ? __( '(ex. VAT)', 'woocommerce' ) : __( '(ex. tax)', 'woocommerce' );
  391. return apply_filters( 'woocommerce_countries_ex_tax_or_vat', $return );
  392. }
  393. /**
  394. * Outputs the list of countries and states for use in dropdown boxes.
  395. *
  396. * @param string $selected_country Selected country.
  397. * @param string $selected_state Selected state.
  398. * @param bool $escape If we should escape HTML.
  399. */
  400. public function country_dropdown_options( $selected_country = '', $selected_state = '', $escape = false ) {
  401. if ( $this->countries ) {
  402. foreach ( $this->countries as $key => $value ) {
  403. $states = $this->get_states( $key );
  404. if ( $states ) {
  405. echo '<optgroup label="' . esc_attr( $value ) . '">';
  406. foreach ( $states as $state_key => $state_value ) {
  407. echo '<option value="' . esc_attr( $key ) . ':' . esc_attr( $state_key ) . '"';
  408. if ( $selected_country === $key && $selected_state === $state_key ) {
  409. echo ' selected="selected"';
  410. }
  411. echo '>' . esc_html( $value ) . ' &mdash; ' . ( $escape ? esc_html( $state_value ) : $state_value ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  412. }
  413. echo '</optgroup>';
  414. } else {
  415. echo '<option';
  416. if ( $selected_country === $key && '*' === $selected_state ) {
  417. echo ' selected="selected"';
  418. }
  419. echo ' value="' . esc_attr( $key ) . '">' . ( $escape ? esc_html( $value ) : $value ) . '</option>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * Get country address formats.
  426. *
  427. * These define how addresses are formatted for display in various countries.
  428. *
  429. * @return array
  430. */
  431. public function get_address_formats() {
  432. if ( empty( $this->address_formats ) ) {
  433. $this->address_formats = apply_filters(
  434. 'woocommerce_localisation_address_formats',
  435. array(
  436. 'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
  437. 'AT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  438. 'AU' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {state} {postcode}\n{country}",
  439. 'BE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  440. 'CA' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {state_code} {postcode}\n{country}",
  441. 'CH' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  442. 'CL' => "{company}\n{name}\n{address_1}\n{address_2}\n{state}\n{postcode} {city}\n{country}",
  443. 'CN' => "{country} {postcode}\n{state}, {city}, {address_2}, {address_1}\n{company}\n{name}",
  444. 'CZ' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  445. 'DE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  446. 'DK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  447. 'EE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  448. 'ES' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}",
  449. 'FI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  450. 'FR' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city_upper}\n{country}",
  451. 'HK' => "{company}\n{first_name} {last_name_upper}\n{address_1}\n{address_2}\n{city_upper}\n{state_upper}\n{country}",
  452. 'HU' => "{last_name} {first_name}\n{company}\n{city}\n{address_1}\n{address_2}\n{postcode}\n{country}",
  453. 'IN' => "{company}\n{name}\n{address_1}\n{address_2}\n{city} {postcode}\n{state}, {country}",
  454. 'IS' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  455. 'IT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode}\n{city}\n{state_upper}\n{country}",
  456. 'JM' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode_upper}\n{country}",
  457. 'JP' => "{postcode}\n{state} {city} {address_1}\n{address_2}\n{company}\n{last_name} {first_name}\n{country}",
  458. 'LI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  459. 'NL' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  460. 'NO' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  461. 'NZ' => "{name}\n{company}\n{address_1}\n{address_2}\n{city} {postcode}\n{country}",
  462. 'PL' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  463. 'PR' => "{company}\n{name}\n{address_1} {address_2}\n{city} \n{country} {postcode}",
  464. 'PT' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  465. 'RS' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  466. 'SE' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  467. 'SI' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  468. 'SK' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",
  469. 'TR' => "{name}\n{company}\n{address_1}\n{address_2}\n{postcode} {city} {state}\n{country}",
  470. 'TW' => "{company}\n{last_name} {first_name}\n{address_1}\n{address_2}\n{state}, {city} {postcode}\n{country}",
  471. 'UG' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}, {country}",
  472. 'US' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state_code} {postcode}\n{country}",
  473. 'VN' => "{name}\n{company}\n{address_1}\n{city}\n{country}",
  474. )
  475. );
  476. }
  477. return $this->address_formats;
  478. }
  479. /**
  480. * Get country address format.
  481. *
  482. * @param array $args Arguments.
  483. * @param string $separator How to separate address lines. @since 3.5.0.
  484. * @return string
  485. */
  486. public function get_formatted_address( $args = array(), $separator = '<br/>' ) {
  487. $default_args = array(
  488. 'first_name' => '',
  489. 'last_name' => '',
  490. 'company' => '',
  491. 'address_1' => '',
  492. 'address_2' => '',
  493. 'city' => '',
  494. 'state' => '',
  495. 'postcode' => '',
  496. 'country' => '',
  497. );
  498. $args = array_map( 'trim', wp_parse_args( $args, $default_args ) );
  499. $state = $args['state'];
  500. $country = $args['country'];
  501. // Get all formats.
  502. $formats = $this->get_address_formats();
  503. // Get format for the address' country.
  504. $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
  505. // Handle full country name.
  506. $full_country = ( isset( $this->countries[ $country ] ) ) ? $this->countries[ $country ] : $country;
  507. // Country is not needed if the same as base.
  508. if ( $country === $this->get_base_country() && ! apply_filters( 'woocommerce_formatted_address_force_country_display', false ) ) {
  509. $format = str_replace( '{country}', '', $format );
  510. }
  511. // Handle full state name.
  512. $full_state = ( $country && $state && isset( $this->states[ $country ][ $state ] ) ) ? $this->states[ $country ][ $state ] : $state;
  513. // Substitute address parts into the string.
  514. $replace = array_map(
  515. 'esc_html',
  516. apply_filters(
  517. 'woocommerce_formatted_address_replacements',
  518. array(
  519. '{first_name}' => $args['first_name'],
  520. '{last_name}' => $args['last_name'],
  521. '{name}' => sprintf(
  522. /* translators: 1: first name 2: last name */
  523. _x( '%1$s %2$s', 'full name', 'woocommerce' ),
  524. $args['first_name'],
  525. $args['last_name']
  526. ),
  527. '{company}' => $args['company'],
  528. '{address_1}' => $args['address_1'],
  529. '{address_2}' => $args['address_2'],
  530. '{city}' => $args['city'],
  531. '{state}' => $full_state,
  532. '{postcode}' => $args['postcode'],
  533. '{country}' => $full_country,
  534. '{first_name_upper}' => wc_strtoupper( $args['first_name'] ),
  535. '{last_name_upper}' => wc_strtoupper( $args['last_name'] ),
  536. '{name_upper}' => wc_strtoupper(
  537. sprintf(
  538. /* translators: 1: first name 2: last name */
  539. _x( '%1$s %2$s', 'full name', 'woocommerce' ),
  540. $args['first_name'],
  541. $args['last_name']
  542. )
  543. ),
  544. '{company_upper}' => wc_strtoupper( $args['company'] ),
  545. '{address_1_upper}' => wc_strtoupper( $args['address_1'] ),
  546. '{address_2_upper}' => wc_strtoupper( $args['address_2'] ),
  547. '{city_upper}' => wc_strtoupper( $args['city'] ),
  548. '{state_upper}' => wc_strtoupper( $full_state ),
  549. '{state_code}' => wc_strtoupper( $state ),
  550. '{postcode_upper}' => wc_strtoupper( $args['postcode'] ),
  551. '{country_upper}' => wc_strtoupper( $full_country ),
  552. ),
  553. $args
  554. )
  555. );
  556. $formatted_address = str_replace( array_keys( $replace ), $replace, $format );
  557. // Clean up white space.
  558. $formatted_address = preg_replace( '/ +/', ' ', trim( $formatted_address ) );
  559. $formatted_address = preg_replace( '/\n\n+/', "\n", $formatted_address );
  560. // Break newlines apart and remove empty lines/trim commas and white space.
  561. $formatted_address = array_filter( array_map( array( $this, 'trim_formatted_address_line' ), explode( "\n", $formatted_address ) ) );
  562. // Add html breaks.
  563. $formatted_address = implode( $separator, $formatted_address );
  564. // We're done!
  565. return $formatted_address;
  566. }
  567. /**
  568. * Trim white space and commas off a line.
  569. *
  570. * @param string $line Line.
  571. * @return string
  572. */
  573. private function trim_formatted_address_line( $line ) {
  574. return trim( $line, ', ' );
  575. }
  576. /**
  577. * Returns the fields we show by default. This can be filtered later on.
  578. *
  579. * @return array
  580. */
  581. public function get_default_address_fields() {
  582. $address_2_label = __( 'Apartment, suite, unit, etc.', 'woocommerce' );
  583. // If necessary, append '(optional)' to the placeholder: we don't need to worry about the
  584. // label, though, as woocommerce_form_field() takes care of that.
  585. if ( 'optional' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
  586. $address_2_placeholder = __( 'Apartment, suite, unit, etc. (optional)', 'woocommerce' );
  587. } else {
  588. $address_2_placeholder = $address_2_label;
  589. }
  590. $fields = array(
  591. 'first_name' => array(
  592. 'label' => __( 'First name', 'woocommerce' ),
  593. 'required' => true,
  594. 'class' => array( 'form-row-first' ),
  595. 'autocomplete' => 'given-name',
  596. 'priority' => 10,
  597. ),
  598. 'last_name' => array(
  599. 'label' => __( 'Last name', 'woocommerce' ),
  600. 'required' => true,
  601. 'class' => array( 'form-row-last' ),
  602. 'autocomplete' => 'family-name',
  603. 'priority' => 20,
  604. ),
  605. 'company' => array(
  606. 'label' => __( 'Company name', 'woocommerce' ),
  607. 'class' => array( 'form-row-wide' ),
  608. 'autocomplete' => 'organization',
  609. 'priority' => 30,
  610. 'required' => 'required' === get_option( 'woocommerce_checkout_company_field', 'optional' ),
  611. ),
  612. 'country' => array(
  613. 'type' => 'country',
  614. 'label' => __( 'Country / Region', 'woocommerce' ),
  615. 'required' => true,
  616. 'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
  617. 'autocomplete' => 'country',
  618. 'priority' => 40,
  619. ),
  620. 'address_1' => array(
  621. 'label' => __( 'Street address', 'woocommerce' ),
  622. /* translators: use local order of street name and house number. */
  623. 'placeholder' => esc_attr__( 'House number and street name', 'woocommerce' ),
  624. 'required' => true,
  625. 'class' => array( 'form-row-wide', 'address-field' ),
  626. 'autocomplete' => 'address-line1',
  627. 'priority' => 50,
  628. ),
  629. 'address_2' => array(
  630. 'label' => $address_2_label,
  631. 'label_class' => array( 'screen-reader-text' ),
  632. 'placeholder' => esc_attr( $address_2_placeholder ),
  633. 'class' => array( 'form-row-wide', 'address-field' ),
  634. 'autocomplete' => 'address-line2',
  635. 'priority' => 60,
  636. 'required' => 'required' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ),
  637. ),
  638. 'city' => array(
  639. 'label' => __( 'Town / City', 'woocommerce' ),
  640. 'required' => true,
  641. 'class' => array( 'form-row-wide', 'address-field' ),
  642. 'autocomplete' => 'address-level2',
  643. 'priority' => 70,
  644. ),
  645. 'state' => array(
  646. 'type' => 'state',
  647. 'label' => __( 'State / County', 'woocommerce' ),
  648. 'required' => true,
  649. 'class' => array( 'form-row-wide', 'address-field' ),
  650. 'validate' => array( 'state' ),
  651. 'autocomplete' => 'address-level1',
  652. 'priority' => 80,
  653. ),
  654. 'postcode' => array(
  655. 'label' => __( 'Postcode / ZIP', 'woocommerce' ),
  656. 'required' => true,
  657. 'class' => array( 'form-row-wide', 'address-field' ),
  658. 'validate' => array( 'postcode' ),
  659. 'autocomplete' => 'postal-code',
  660. 'priority' => 90,
  661. ),
  662. );
  663. if ( 'hidden' === get_option( 'woocommerce_checkout_company_field', 'optional' ) ) {
  664. unset( $fields['company'] );
  665. }
  666. if ( 'hidden' === get_option( 'woocommerce_checkout_address_2_field', 'optional' ) ) {
  667. unset( $fields['address_2'] );
  668. }
  669. $default_address_fields = apply_filters( 'woocommerce_default_address_fields', $fields );
  670. // Sort each of the fields based on priority.
  671. uasort( $default_address_fields, 'wc_checkout_fields_uasort_comparison' );
  672. return $default_address_fields;
  673. }
  674. /**
  675. * Get JS selectors for fields which are shown/hidden depending on the locale.
  676. *
  677. * @return array
  678. */
  679. public function get_country_locale_field_selectors() {
  680. $locale_fields = array(
  681. 'address_1' => '#billing_address_1_field, #shipping_address_1_field',
  682. 'address_2' => '#billing_address_2_field, #shipping_address_2_field',
  683. 'state' => '#billing_state_field, #shipping_state_field, #calc_shipping_state_field',
  684. 'postcode' => '#billing_postcode_field, #shipping_postcode_field, #calc_shipping_postcode_field',
  685. 'city' => '#billing_city_field, #shipping_city_field, #calc_shipping_city_field',
  686. );
  687. return apply_filters( 'woocommerce_country_locale_field_selectors', $locale_fields );
  688. }
  689. /**
  690. * Get country locale settings.
  691. *
  692. * These locales override the default country selections after a country is chosen.
  693. *
  694. * @return array
  695. */
  696. public function get_country_locale() {
  697. if ( empty( $this->locale ) ) {
  698. $this->locale = apply_filters(
  699. 'woocommerce_get_country_locale',
  700. array(
  701. 'AE' => array(
  702. 'postcode' => array(
  703. 'required' => false,
  704. 'hidden' => true,
  705. ),
  706. 'state' => array(
  707. 'required' => false,
  708. ),
  709. ),
  710. 'AF' => array(
  711. 'state' => array(
  712. 'required' => false,
  713. 'hidden' => true,
  714. ),
  715. ),
  716. 'AO' => array(
  717. 'postcode' => array(
  718. 'required' => false,
  719. 'hidden' => true,
  720. ),
  721. 'state' => array(
  722. 'label' => __( 'Province', 'woocommerce' ),
  723. ),
  724. ),
  725. 'AT' => array(
  726. 'postcode' => array(
  727. 'priority' => 65,
  728. ),
  729. 'state' => array(
  730. 'required' => false,
  731. 'hidden' => true,
  732. ),
  733. ),
  734. 'AU' => array(
  735. 'city' => array(
  736. 'label' => __( 'Suburb', 'woocommerce' ),
  737. ),
  738. 'postcode' => array(
  739. 'label' => __( 'Postcode', 'woocommerce' ),
  740. ),
  741. 'state' => array(
  742. 'label' => __( 'State', 'woocommerce' ),
  743. ),
  744. ),
  745. 'AX' => array(
  746. 'postcode' => array(
  747. 'priority' => 65,
  748. ),
  749. 'state' => array(
  750. 'required' => false,
  751. 'hidden' => true,
  752. ),
  753. ),
  754. 'BA' => array(
  755. 'postcode' => array(
  756. 'priority' => 65,
  757. ),
  758. 'state' => array(
  759. 'label' => __( 'Canton', 'woocommerce' ),
  760. 'required' => false,
  761. 'hidden' => true,
  762. ),
  763. ),
  764. 'BD' => array(
  765. 'postcode' => array(
  766. 'required' => false,
  767. ),
  768. 'state' => array(
  769. 'label' => __( 'District', 'woocommerce' ),
  770. ),
  771. ),
  772. 'BE' => array(
  773. 'postcode' => array(
  774. 'priority' => 65,
  775. ),
  776. 'state' => array(
  777. 'required' => false,
  778. 'hidden' => true,
  779. ),
  780. ),
  781. 'BH' => array(
  782. 'postcode' => array(
  783. 'required' => false,
  784. ),
  785. 'state' => array(
  786. 'required' => false,
  787. 'hidden' => true,
  788. ),
  789. ),
  790. 'BI' => array(
  791. 'state' => array(
  792. 'required' => false,
  793. 'hidden' => true,
  794. ),
  795. ),
  796. 'BO' => array(
  797. 'postcode' => array(
  798. 'required' => false,
  799. 'hidden' => true,
  800. ),
  801. ),
  802. 'BS' => array(
  803. 'postcode' => array(
  804. 'required' => false,
  805. 'hidden' => true,
  806. ),
  807. ),
  808. 'CA' => array(
  809. 'postcode' => array(
  810. 'label' => __( 'Postal code', 'woocommerce' ),
  811. ),
  812. 'state' => array(
  813. 'label' => __( 'Province', 'woocommerce' ),
  814. ),
  815. ),
  816. 'CH' => array(
  817. 'postcode' => array(
  818. 'priority' => 65,
  819. ),
  820. 'state' => array(
  821. 'label' => __( 'Canton', 'woocommerce' ),
  822. 'required' => false,
  823. ),
  824. ),
  825. 'CL' => array(
  826. 'city' => array(
  827. 'required' => true,
  828. ),
  829. 'postcode' => array(
  830. 'required' => false,
  831. ),
  832. 'state' => array(
  833. 'label' => __( 'Region', 'woocommerce' ),
  834. ),
  835. ),
  836. 'CN' => array(
  837. 'state' => array(
  838. 'label' => __( 'Province', 'woocommerce' ),
  839. ),
  840. ),
  841. 'CO' => array(
  842. 'postcode' => array(
  843. 'required' => false,
  844. ),
  845. ),
  846. 'CW' => array(
  847. 'postcode' => array(
  848. 'required' => false,
  849. 'hidden' => true,
  850. ),
  851. 'state' => array(
  852. 'required' => false,
  853. ),
  854. ),
  855. 'CZ' => array(
  856. 'state' => array(
  857. 'required' => false,
  858. 'hidden' => true,
  859. ),
  860. ),
  861. 'DE' => array(
  862. 'postcode' => array(
  863. 'priority' => 65,
  864. ),
  865. 'state' => array(
  866. 'required' => false,
  867. 'hidden' => true,
  868. ),
  869. ),
  870. 'DK' => array(
  871. 'postcode' => array(
  872. 'priority' => 65,
  873. ),
  874. 'state' => array(
  875. 'required' => false,
  876. 'hidden' => true,
  877. ),
  878. ),
  879. 'EE' => array(
  880. 'postcode' => array(
  881. 'priority' => 65,
  882. ),
  883. 'state' => array(
  884. 'required' => false,
  885. 'hidden' => true,
  886. ),
  887. ),
  888. 'FI' => array(
  889. 'postcode' => array(
  890. 'priority' => 65,
  891. ),
  892. 'state' => array(
  893. 'required' => false,
  894. 'hidden' => true,
  895. ),
  896. ),
  897. 'FR' => array(
  898. 'postcode' => array(
  899. 'priority' => 65,
  900. ),
  901. 'state' => array(
  902. 'required' => false,
  903. 'hidden' => true,
  904. ),
  905. ),
  906. 'GH' => array(
  907. 'postcode' => array(
  908. 'required' => false,
  909. ),
  910. 'state' => array(
  911. 'label' => __( 'Region', 'woocommerce' ),
  912. ),
  913. ),
  914. 'GP' => array(
  915. 'state' => array(
  916. 'required' => false,
  917. 'hidden' => true,
  918. ),
  919. ),
  920. 'GF' => array(
  921. 'state' => array(
  922. 'required' => false,
  923. 'hidden' => true,
  924. ),
  925. ),
  926. 'GR' => array(
  927. 'state' => array(
  928. 'required' => false,
  929. ),
  930. ),
  931. 'GT' => array(
  932. 'postcode' => array(
  933. 'required' => false,
  934. 'hidden' => true,
  935. ),
  936. 'state' => array(
  937. 'label' => __( 'Department', 'woocommerce' ),
  938. ),
  939. ),
  940. 'HK' => array(
  941. 'postcode' => array(
  942. 'required' => false,
  943. ),
  944. 'city' => array(
  945. 'label' => __( 'Town / District', 'woocommerce' ),
  946. ),
  947. 'state' => array(
  948. 'label' => __( 'Region', 'woocommerce' ),
  949. ),
  950. ),
  951. 'HU' => array(
  952. 'last_name' => array(
  953. 'class' => array( 'form-row-first' ),
  954. 'priority' => 10,
  955. ),
  956. 'first_name' => array(
  957. 'class' => array( 'form-row-last' ),
  958. 'priority' => 20,
  959. ),
  960. 'postcode' => array(
  961. 'class' => array( 'form-row-first', 'address-field' ),
  962. 'priority' => 65,
  963. ),
  964. 'city' => array(
  965. 'class' => array( 'form-row-last', 'address-field' ),
  966. ),
  967. 'address_1' => array(
  968. 'priority' => 71,
  969. ),
  970. 'address_2' => array(
  971. 'priority' => 72,
  972. ),
  973. 'state' => array(
  974. 'label' => __( 'County', 'woocommerce' ),
  975. ),
  976. ),
  977. 'ID' => array(
  978. 'state' => array(
  979. 'label' => __( 'Province', 'woocommerce' ),
  980. ),
  981. ),
  982. 'IE' => array(
  983. 'postcode' => array(
  984. 'required' => false,
  985. 'label' => __( 'Eircode', 'woocommerce' ),
  986. ),
  987. 'state' => array(
  988. 'label' => __( 'County', 'woocommerce' ),
  989. ),
  990. ),
  991. 'IS' => array(
  992. 'postcode' => array(
  993. 'priority' => 65,
  994. ),
  995. 'state' => array(
  996. 'required' => false,
  997. 'hidden' => true,
  998. ),
  999. ),
  1000. 'IL' => array(
  1001. 'postcode' => array(
  1002. 'priority' => 65,
  1003. ),
  1004. 'state' => array(
  1005. 'required' => false,
  1006. 'hidden' => true,
  1007. ),
  1008. ),
  1009. 'IM' => array(
  1010. 'state' => array(
  1011. 'required' => false,
  1012. 'hidden' => true,
  1013. ),
  1014. ),
  1015. 'IN' => array(
  1016. 'postcode' => array(
  1017. 'label' => __( 'PIN', 'woocommerce' ),
  1018. ),
  1019. 'state' => array(
  1020. 'label' => __( 'State', 'woocommerce' ),
  1021. ),
  1022. ),
  1023. 'IT' => array(
  1024. 'postcode' => array(
  1025. 'priority' => 65,
  1026. ),
  1027. 'state' => array(
  1028. 'required' => true,
  1029. 'label' => __( 'Province', 'woocommerce' ),
  1030. ),
  1031. ),
  1032. 'JM' => array(
  1033. 'city' => array(
  1034. 'label' => __( 'Town / City / Post Office', 'woocommerce' ),
  1035. ),
  1036. 'postcode' => array(
  1037. 'required' => false,
  1038. 'label' => __( 'Postal Code', 'woocommerce' ),
  1039. ),
  1040. 'state' => array(
  1041. 'required' => true,
  1042. 'label' => __( 'Parish', 'woocommerce' ),
  1043. ),
  1044. ),
  1045. 'JP' => array(
  1046. 'last_name' => array(
  1047. 'class' => array( 'form-row-first' ),
  1048. 'priority' => 10,
  1049. ),
  1050. 'first_name' => array(
  1051. 'class' => array( 'form-row-last' ),
  1052. 'priority' => 20,
  1053. ),
  1054. 'postcode' => array(
  1055. 'class' => array( 'form-row-first', 'address-field' ),
  1056. 'priority' => 65,
  1057. ),
  1058. 'state' => array(
  1059. 'label' => __( 'Prefecture', 'woocommerce' ),
  1060. 'class' => array( 'form-row-last', 'address-field' ),
  1061. 'priority' => 66,
  1062. ),
  1063. 'city' => array(
  1064. 'priority' => 67,
  1065. ),
  1066. 'address_1' => array(
  1067. 'priority' => 68,
  1068. ),
  1069. 'address_2' => array(
  1070. 'priority' => 69,
  1071. ),
  1072. ),
  1073. 'KR' => array(
  1074. 'state' => array(
  1075. 'required' => false,
  1076. 'hidden' => true,
  1077. ),
  1078. ),
  1079. 'KW' => array(
  1080. 'state' => array(
  1081. 'required' => false,
  1082. 'hidden' => true,
  1083. ),
  1084. ),
  1085. 'LV' => array(
  1086. 'state' => array(
  1087. 'label' => __( 'Municipality', 'woocommerce' ),
  1088. 'required' => false,
  1089. ),
  1090. ),
  1091. 'LB' => array(
  1092. 'state' => array(
  1093. 'required' => false,
  1094. 'hidden' => true,
  1095. ),
  1096. ),
  1097. 'MQ' => array(
  1098. 'state' => array(
  1099. 'required' => false,
  1100. 'hidden' => true,
  1101. ),
  1102. ),
  1103. 'MT' => array(
  1104. 'state' => array(
  1105. 'required' => false,
  1106. 'hidden' => true,
  1107. ),
  1108. ),
  1109. 'MZ' => array(
  1110. 'postcode' => array(
  1111. 'required' => false,
  1112. 'hidden' => true,
  1113. ),
  1114. 'state' => array(
  1115. 'label' => __( 'Province', 'woocommerce' ),
  1116. ),
  1117. ),
  1118. 'NL' => array(
  1119. 'postcode' => array(
  1120. 'priority' => 65,
  1121. ),
  1122. 'state' => array(
  1123. 'required' => false,
  1124. 'hidden' => true,
  1125. ),
  1126. ),
  1127. 'NG' => array(
  1128. 'postcode' => array(
  1129. 'label' => __( 'Postcode', 'woocommerce' ),
  1130. 'required' => false,
  1131. 'hidden' => true,
  1132. ),
  1133. 'state' => array(
  1134. 'label' => __( 'State', 'woocommerce' ),
  1135. ),
  1136. ),
  1137. 'NZ' => array(
  1138. 'postcode' => array(
  1139. 'label' => __( 'Postcode', 'woocommerce' ),
  1140. ),
  1141. 'state' => array(
  1142. 'required' => false,
  1143. 'label' => __( 'Region', 'woocommerce' ),
  1144. ),
  1145. ),
  1146. 'NO' => array(
  1147. 'postcode' => array(
  1148. 'priority' => 65,
  1149. ),
  1150. 'state' => array(
  1151. 'required' => false,
  1152. 'hidden' => true,
  1153. ),
  1154. ),
  1155. 'NP' => array(
  1156. 'state' => array(
  1157. 'label' => __( 'State / Zone', 'woocommerce' ),
  1158. ),
  1159. 'postcode' => array(
  1160. 'required' => false,
  1161. ),
  1162. ),
  1163. 'PL' => array(
  1164. 'postcode' => array(
  1165. 'priority' => 65,
  1166. ),
  1167. 'state' => array(
  1168. 'required' => false,
  1169. 'hidden' => true,
  1170. ),
  1171. ),
  1172. 'PR' => array(
  1173. 'city' => array(
  1174. 'label' => __( 'Municipality', 'woocommerce' ),
  1175. ),
  1176. 'state' => array(
  1177. 'required' => false,
  1178. 'hidden' => true,
  1179. ),
  1180. ),
  1181. 'PT' => array(
  1182. 'state' => array(
  1183. 'required' => false,
  1184. 'hidden' => true,
  1185. ),
  1186. ),
  1187. 'RE' => array(
  1188. 'state' => array(
  1189. 'required' => false,
  1190. 'hidden' => true,
  1191. ),
  1192. ),
  1193. 'RO' => array(
  1194. 'state' => array(
  1195. 'label' => __( 'County', 'woocommerce' ),
  1196. 'required' => true,
  1197. ),
  1198. ),
  1199. 'RS' => array(
  1200. 'city' => array(
  1201. 'required' => true,
  1202. ),
  1203. 'postcode' => array(
  1204. 'required' => true,
  1205. ),
  1206. 'state' => array(
  1207. 'label' => __( 'District', 'woocommerce' ),
  1208. 'required' => false,
  1209. ),
  1210. ),
  1211. 'SG' => array(
  1212. 'state' => array(
  1213. 'required' => false,
  1214. 'hidden' => true,
  1215. ),
  1216. 'city' => array(
  1217. 'required' => false,
  1218. ),
  1219. ),
  1220. 'SK' => array(
  1221. 'postcode' => array(
  1222. 'priority' => 65,
  1223. ),
  1224. 'state' => array(
  1225. 'required' => false,
  1226. 'hidden' => true,
  1227. ),
  1228. ),
  1229. 'SI' => array(
  1230. 'postcode' => array(
  1231. 'priority' => 65,
  1232. ),
  1233. 'state' => array(
  1234. 'required' => false,
  1235. 'hidden' => true,
  1236. ),
  1237. ),
  1238. 'SR' => array(
  1239. 'postcode' => array(
  1240. 'required' => false,
  1241. 'hidden' => true,
  1242. ),
  1243. ),
  1244. 'ES' => array(
  1245. 'postcode' => array(
  1246. 'priority' => 65,
  1247. ),
  1248. 'state' => array(
  1249. 'label' => __( 'Province', 'woocommerce' ),
  1250. ),
  1251. ),
  1252. 'LI' => array(
  1253. 'postcode' => array(
  1254. 'priority' => 65,
  1255. ),
  1256. 'state' => array(
  1257. 'label' => __( 'Municipality', 'woocommerce' ),
  1258. 'required' => false,
  1259. ),
  1260. ),
  1261. 'LK' => array(
  1262. 'state' => array(
  1263. 'required' => false,
  1264. 'hidden' => true,
  1265. ),
  1266. ),
  1267. 'LU' => array(
  1268. 'state' => array(
  1269. 'required' => false,
  1270. 'hidden' => true,
  1271. ),
  1272. ),
  1273. 'MD' => array(
  1274. 'state' => array(
  1275. 'label' => __( 'Municipality / District', 'woocommerce' ),
  1276. ),
  1277. ),
  1278. 'SE' => array(
  1279. 'postcode' => array(
  1280. 'priority' => 65,
  1281. ),
  1282. 'state' => array(
  1283. 'required' => false,
  1284. 'hidden' => true,
  1285. ),
  1286. ),
  1287. 'TR' => array(
  1288. 'postcode' => array(
  1289. 'priority' => 65,
  1290. ),
  1291. 'state' => array(
  1292. 'label' => __( 'Province', 'woocommerce' ),
  1293. ),
  1294. ),
  1295. 'UG' => array(
  1296. 'postcode' => array(
  1297. 'required' => false,
  1298. 'hidden' => true,
  1299. ),
  1300. 'city' => array(
  1301. 'label' => __( 'Town / Village', 'woocommerce' ),
  1302. 'required' => true,
  1303. ),
  1304. 'state' => array(
  1305. 'label' => __( 'District', 'woocommerce' ),
  1306. 'required' => true,
  1307. ),
  1308. ),
  1309. 'US' => array(
  1310. 'postcode' => array(
  1311. 'label' => __( 'ZIP Code', 'woocommerce' ),
  1312. ),
  1313. 'state' => array(
  1314. 'label' => __( 'State', 'woocommerce' ),
  1315. ),
  1316. ),
  1317. 'GB' => array(
  1318. 'postcode' => array(
  1319. 'label' => __( 'Postcode', 'woocommerce' ),
  1320. ),
  1321. 'state' => array(
  1322. 'label' => __( 'County', 'woocommerce' ),
  1323. 'required' => false,
  1324. ),
  1325. ),
  1326. 'ST' => array(
  1327. 'postcode' => array(
  1328. 'required' => false,
  1329. 'hidden' => true,
  1330. ),
  1331. 'state' => array(
  1332. 'label' => __( 'District', 'woocommerce' ),
  1333. ),
  1334. ),
  1335. 'VN' => array(
  1336. 'state' => array(
  1337. 'required' => false,
  1338. 'hidden' => true,
  1339. ),
  1340. 'postcode' => array(
  1341. 'priority' => 65,
  1342. 'required' => false,
  1343. 'hidden' => false,
  1344. ),
  1345. 'address_2' => array(
  1346. 'required' => false,
  1347. 'hidden' => true,
  1348. ),
  1349. ),
  1350. 'WS' => array(
  1351. 'postcode' => array(
  1352. 'required' => false,
  1353. 'hidden' => true,
  1354. ),
  1355. ),
  1356. 'YT' => array(
  1357. 'state' => array(
  1358. 'required' => false,
  1359. 'hidden' => true,
  1360. ),
  1361. ),
  1362. 'ZA' => array(
  1363. 'state' => array(
  1364. 'label' => __( 'Province', 'woocommerce' ),
  1365. ),
  1366. ),
  1367. 'ZW' => array(
  1368. 'postcode' => array(
  1369. 'required' => false,
  1370. 'hidden' => true,
  1371. ),
  1372. ),
  1373. )
  1374. );
  1375. $this->locale = array_intersect_key( $this->locale, array_merge( $this->get_allowed_countries(), $this->get_shipping_countries() ) );
  1376. // Default Locale Can be filtered to override fields in get_address_fields(). Countries with no specific locale will use default.
  1377. $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_default', $this->get_default_address_fields() );
  1378. // Filter default AND shop base locales to allow overides via a single function. These will be used when changing countries on the checkout.
  1379. if ( ! isset( $this->locale[ $this->get_base_country() ] ) ) {
  1380. $this->locale[ $this->get_base_country() ] = $this->locale['default'];
  1381. }
  1382. $this->locale['default'] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale['default'] );
  1383. $this->locale[ $this->get_base_country() ] = apply_filters( 'woocommerce_get_country_locale_base', $this->locale[ $this->get_base_country() ] );
  1384. }
  1385. return $this->locale;
  1386. }
  1387. /**
  1388. * Apply locale and get address fields.
  1389. *
  1390. * @param mixed $country Country.
  1391. * @param string $type Address type, defaults to 'billing_'.
  1392. * @return array
  1393. */
  1394. public function get_address_fields( $country = '', $type = 'billing_' ) {
  1395. if ( ! $country ) {
  1396. $country = $this->get_base_country();
  1397. }
  1398. $fields = $this->get_default_address_fields();
  1399. $locale = $this->get_country_locale();
  1400. if ( isset( $locale[ $country ] ) ) {
  1401. $fields = wc_array_overlay( $fields, $locale[ $country ] );
  1402. }
  1403. // Prepend field keys.
  1404. $address_fields = array();
  1405. foreach ( $fields as $key => $value ) {
  1406. if ( 'state' === $key ) {
  1407. $value['country_field'] = $type . 'country';
  1408. $value['country'] = $country;
  1409. }
  1410. $address_fields[ $type . $key ] = $value;
  1411. }
  1412. // Add email and phone fields.
  1413. if ( 'billing_' === $type ) {
  1414. if ( 'hidden' !== get_option( 'woocommerce_checkout_phone_field', 'required' ) ) {
  1415. $address_fields['billing_phone'] = array(
  1416. 'label' => __( 'Phone', 'woocommerce' ),
  1417. 'required' => 'required' === get_option( 'woocommerce_checkout_phone_field', 'required' ),
  1418. 'type' => 'tel',
  1419. 'class' => array( 'form-row-wide' ),
  1420. 'validate' => array( 'phone' ),
  1421. 'autocomplete' => 'tel',
  1422. 'priority' => 100,
  1423. );
  1424. }
  1425. $address_fields['billing_email'] = array(
  1426. 'label' => __( 'Email address', 'woocommerce' ),
  1427. 'required' => true,
  1428. 'type' => 'email',
  1429. 'class' => array( 'form-row-wide' ),
  1430. 'validate' => array( 'email' ),
  1431. 'autocomplete' => 'no' === get_option( 'woocommerce_registration_generate_username' ) ? 'email' : 'email username',
  1432. 'priority' => 110,
  1433. );
  1434. }
  1435. /**
  1436. * Important note on this filter: Changes to address fields can and will be overridden by
  1437. * the woocommerce_default_address_fields. The locales/default locales apply on top based
  1438. * on country selection. If you want to change things like the required status of an
  1439. * address field, filter woocommerce_default_address_fields instead.
  1440. */
  1441. $address_fields = apply_filters( 'woocommerce_' . $type . 'fields', $address_fields, $country );
  1442. // Sort each of the fields based on priority.
  1443. uasort( $address_fields, 'wc_checkout_fields_uasort_comparison' );
  1444. return $address_fields;
  1445. }
  1446. }