WC_Countries
in package
The WooCommerce countries class stores country/state data.
Table of Contents
- $address_formats : array<string|int, mixed>
- List of address formats for locales.
- $locale : array<string|int, mixed>
- Locales list.
- $states : array<string|int, mixed>
- $country_locale_building : bool
- Whether get_country_locale() is building the country locale settings.
- $country_locale_built_for : string|null
- Locale used to build the cached country locale settings.
- $geo_cache : array<string|int, mixed>
- Cache of geographical regions.
- $geo_cache_building : array<string, array<string, bool>>
- Geographical cache entries being built, keyed by data type and locale.
- $last_cache_locale : string|null
- Locale most recently resolved by get_cache_locale().
- $locale_resolution_snapshots : array<int, array{0: \WC_Countries, 1: array}>|null
- Geographical caches to restore when the current locale resolution ends, keyed by object ID.
- __get() : mixed
- Auto-load in-accessible properties on demand.
- countries_using_vat() : array<string|int, string>
- 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
- country_dropdown_options() : mixed
- Outputs the list of countries and states for use in dropdown boxes.
- country_exists() : bool
- Check if a given code represents a valid ISO 3166-1 alpha-2 code for a country known to us.
- estimated_for_prefix() : string
- Prefix certain countries with 'the'.
- ex_tax_or_vat() : string
- Include the Ex Tax label.
- get_address_fields() : array<string|int, mixed>
- Apply locale and get address fields.
- get_address_formats() : array<string|int, mixed>
- Get country address formats.
- get_allowed_countries() : array<string|int, mixed>
- Get countries that the store sells to.
- get_allowed_country_states() : array<string|int, mixed>
- Get allowed country states.
- get_base_address() : string
- Get the base address (first line) for the store.
- get_base_address_2() : string
- Get the base address (second line) for the store.
- get_base_city() : string
- Get the base city for the store.
- get_base_country() : string
- Get the base country for the store.
- get_base_postcode() : string
- Get the base postcode for the store.
- get_base_state() : string
- Get the base state for the store.
- get_continent_code_for_country() : string
- Get continent code for a country code.
- get_continents() : array<string|int, mixed>
- Get all continents.
- get_countries() : array<string|int, mixed>
- Get all countries.
- get_country_calling_code() : string|array<string|int, mixed>
- Get calling code for a country code.
- get_country_from_alpha_3_code() : string|null
- Searches for a valid ISO 3166-1 alpha-2 code using the provided alpha-3 code.
- get_country_locale() : array<string|int, mixed>
- Get country locale settings.
- get_country_locale_field_selectors() : array<string|int, mixed>
- Get JS selectors for fields which are shown/hidden depending on the locale.
- get_default_address_fields() : array<string|int, mixed>
- Returns the fields we show by default. This can be filtered later on.
- get_european_union_countries() : array<string|int, string>
- Gets an array of countries in the EU.
- get_formatted_address() : string
- Get country address format.
- get_shipping_continents() : array<string|int, mixed>
- Get continents that the store ships to.
- get_shipping_countries() : array<string|int, mixed>
- Get countries that the store ships to.
- get_shipping_country_states() : array<string|int, mixed>
- Get shipping country states.
- get_states() : false|array<string|int, mixed>
- Get the states for a country.
- get_vat_countries() : array<string|int, string>
- Gets an array of countries using VAT.
- inc_tax_or_vat() : string
- Include the Inc Tax label.
- load_country_states() : mixed
- Load the states.
- shipping_to_prefix() : string
- Gets the correct string for shipping - either 'to the' or 'to'.
- tax_or_vat() : string
- Correctly name tax in some countries VAT on the frontend.
- get_cache_locale() : string
- Get the active request locale for geographical cache entries.
- read_geo_property() : mixed
- Read the countries or states property from inside the class.
- restore_locale_resolution_snapshots() : void
- Restore the geographical caches that nested lookups changed while the locale was resolving, and end the resolution.
- trim_formatted_address_line() : string
- Trim white space and commas off a line.
Properties
$address_formats
List of address formats for locales.
public
array<string|int, mixed>
$address_formats
= array()
$locale
Locales list.
public
array<string|int, mixed>
$locale
= array()
$states read-only
public
array<string|int, mixed>
$states
Country states.
$country_locale_building
Whether get_country_locale() is building the country locale settings.
private
bool
$country_locale_building
= alse
$country_locale_built_for
Locale used to build the cached country locale settings.
private
string|null
$country_locale_built_for
=
ull
Null until get_country_locale() builds the public locale property.
$geo_cache
Cache of geographical regions.
private
array<string|int, mixed>
$geo_cache
= array()
Only to be used by the get_* and load_* methods, as other methods may expect the regions to be loaded on demand.
$geo_cache_building
Geographical cache entries being built, keyed by data type and locale.
private
array<string, array<string, bool>>
$geo_cache_building
= array()
$last_cache_locale
Locale most recently resolved by get_cache_locale().
private
string|null
$last_cache_locale
=
ull
$locale_resolution_snapshots
Geographical caches to restore when the current locale resolution ends, keyed by object ID.
private
static array<int, array{0: \WC_Countries, 1: array}>|null
$locale_resolution_snapshots
=
ull
Null while no lookup is resolving the request locale.
Methods
__get()
Auto-load in-accessible properties on demand.
public
__get(mixed $key) : mixed
Parameters
- $key : mixed
-
Key.
Return values
mixed —countries_using_vat()
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
public
countries_using_vat() : array<string|int, string>
Tags
Return values
array<string|int, string> —country_dropdown_options()
Outputs the list of countries and states for use in dropdown boxes.
public
country_dropdown_options([string $selected_country = '' ][, string $selected_state = '' ][, bool $escape = false ]) : mixed
Parameters
- $selected_country : string = ''
-
Selected country.
- $selected_state : string = ''
-
Selected state.
- $escape : bool = false
-
If we should escape HTML.
Return values
mixed —country_exists()
Check if a given code represents a valid ISO 3166-1 alpha-2 code for a country known to us.
public
country_exists(string $country_code) : bool
Parameters
- $country_code : string
-
The country code to check as a ISO 3166-1 alpha-2 code.
Tags
Return values
bool — True if the country is known to us, false otherwise.estimated_for_prefix()
Prefix certain countries with 'the'.
public
estimated_for_prefix([string $country_code = '' ]) : string
Parameters
- $country_code : string = ''
-
Country code.
Return values
string —ex_tax_or_vat()
Include the Ex Tax label.
public
ex_tax_or_vat() : string
Return values
string —get_address_fields()
Apply locale and get address fields.
public
get_address_fields([mixed $country = '' ][, string $type = 'billing_' ]) : array<string|int, mixed>
Parameters
- $country : mixed = ''
-
Country.
- $type : string = 'billing_'
-
Address type, defaults to 'billing_'.
Return values
array<string|int, mixed> —get_address_formats()
Get country address formats.
public
get_address_formats() : array<string|int, mixed>
These define how addresses are formatted for display in various countries.
Return values
array<string|int, mixed> —get_allowed_countries()
Get countries that the store sells to.
public
get_allowed_countries() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_allowed_country_states()
Get allowed country states.
public
get_allowed_country_states() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_base_address()
Get the base address (first line) for the store.
public
get_base_address() : string
Tags
Return values
string —get_base_address_2()
Get the base address (second line) for the store.
public
get_base_address_2() : string
Tags
Return values
string —get_base_city()
Get the base city for the store.
public
get_base_city() : string
Tags
Return values
string —get_base_country()
Get the base country for the store.
public
get_base_country() : string
Return values
string —get_base_postcode()
Get the base postcode for the store.
public
get_base_postcode() : string
Tags
Return values
string —get_base_state()
Get the base state for the store.
public
get_base_state() : string
Return values
string —get_continent_code_for_country()
Get continent code for a country code.
public
get_continent_code_for_country(string $cc) : string
Parameters
- $cc : string
-
Country code.
Tags
Return values
string —get_continents()
Get all continents.
public
get_continents() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_countries()
Get all countries.
public
get_countries() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_country_calling_code()
Get calling code for a country code.
public
get_country_calling_code(string $cc) : string|array<string|int, mixed>
Parameters
- $cc : string
-
Country code.
Tags
Return values
string|array<string|int, mixed> — Some countries have multiple. The code will be stripped of - and spaces and always be prefixed with +.get_country_from_alpha_3_code()
Searches for a valid ISO 3166-1 alpha-2 code using the provided alpha-3 code.
public
get_country_from_alpha_3_code(string $country_code) : string|null
Parameters
- $country_code : string
-
The alpha-3 country code to search for.
Tags
Return values
string|null — The alpha-2 country code, or null if not found.get_country_locale()
Get country locale settings.
public
get_country_locale() : array<string|int, mixed>
These locales override the default country selections after a country is chosen.
Return values
array<string|int, mixed> —get_country_locale_field_selectors()
Get JS selectors for fields which are shown/hidden depending on the locale.
public
get_country_locale_field_selectors() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_default_address_fields()
Returns the fields we show by default. This can be filtered later on.
public
get_default_address_fields() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_european_union_countries()
Gets an array of countries in the EU.
public
get_european_union_countries([string $type = '' ]) : array<string|int, string>
Parameters
- $type : string = ''
-
Type of countries to retrieve. Blank for EU member countries. eu_vat for EU VAT countries.
Return values
array<string|int, string> —get_formatted_address()
Get country address format.
public
get_formatted_address([array<string|int, mixed> $args = array() ][, string $separator = '<br/>' ]) : string
Parameters
- $args : array<string|int, mixed> = array()
-
Arguments.
- $separator : string = '<br/>'
-
How to separate address lines. @since 3.5.0.
Return values
string —get_shipping_continents()
Get continents that the store ships to.
public
get_shipping_continents() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —get_shipping_countries()
Get countries that the store ships to.
public
get_shipping_countries() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_shipping_country_states()
Get shipping country states.
public
get_shipping_country_states() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_states()
Get the states for a country.
public
get_states([string $cc = null ]) : false|array<string|int, mixed>
Parameters
- $cc : string = null
-
Country code.
Return values
false|array<string|int, mixed> — of statesget_vat_countries()
Gets an array of countries using VAT.
public
get_vat_countries() : array<string|int, string>
Tags
Return values
array<string|int, string> — of country codes.inc_tax_or_vat()
Include the Inc Tax label.
public
inc_tax_or_vat() : string
Return values
string —load_country_states()
Load the states.
public
load_country_states() : mixed
Tags
Return values
mixed —shipping_to_prefix()
Gets the correct string for shipping - either 'to the' or 'to'.
public
shipping_to_prefix([string $country_code = '' ]) : string
Parameters
- $country_code : string = ''
-
Country code.
Return values
string —tax_or_vat()
Correctly name tax in some countries VAT on the frontend.
public
tax_or_vat() : string
Return values
string —get_cache_locale()
Get the active request locale for geographical cache entries.
private
get_cache_locale() : string
Locale filter callbacks may read country data. While determine_locale() runs, those nested lookups reuse the last resolved locale instead of resolving it again, and whatever they cache is discarded afterwards.
Return values
string —read_geo_property()
Read the countries or states property from inside the class.
private
read_geo_property(string $key) : mixed
Uses a real property when the object has one, as a plain read would. Otherwise calls __get() directly, because PHP returns null for a magic property that is already being read further up the stack (a locale filter can do that).
Parameters
- $key : string
-
Property name.
Return values
mixed —restore_locale_resolution_snapshots()
Restore the geographical caches that nested lookups changed while the locale was resolving, and end the resolution.
private
static restore_locale_resolution_snapshots() : void
This is a separate method so PHPStan does not treat the registry as the empty array set before determine_locale().
Return values
void —trim_formatted_address_line()
Trim white space and commas off a line.
private
trim_formatted_address_line(string $line) : string
Parameters
- $line : string
-
Line.
