I am trying desperately to understand, how WooCommerce translates certain countries county/state names into database field constants and back.
ie.
I have a customer from Greece, who happens to be from a county/state, that I haven't got letters on this keyboard to name.
Apparently even WooCommerce doesn't have letters for it, for in the database, the county/state is also saved as just "D".
What function can I use to revert it to it's frontend form of
I found something like this, but im unsure how to use it.
1). Directly using WC()->countries
object (when available):
$wc_countries = WC()->countries;
2). Using an instance of WC_Countries
object:
$wc_countries = new WC_Countries();
Then you can use any available WC_Countries
method.
Get all shipping countries codes / names in an array and get all Country states codes / names in a multilevel array:
// Get all countries key/names in an array:
$countries = WC()->countries->get_countries();
// Get all country states key/names in a multilevel array:
$country_states = WC()->countries->get_states();
Or:
$wc_countries = new WC_Countries();
// Get all countries key/names in an array:
$countries = $wc_countries->get_countries();
// Get all country states key/names in a multilevel array:
$country_states = $wc_countries->get_states();
So for example if the country code is GR
and the state code is D
:
$wc_countries = new WC_Countries();
$countries = $countries_obj->get_countries();
$country_states = $countries_obj->get_states();
// Get the country name for "GR" country code *(Greece)*:
$country_name = $countries['GR'];
// Get the state name for "D" state code *(Epirus)*:
$state_name = $country_states['GR']['D'];
// Display names:
echo 'Country name: '.$country_name.'<br>State name: '.$state_name;
This will display:
Country name: Greece
State name: Ήπειρος
To display all states for a country, use something like:
$wc_countries = new WC_Countries();
$countries = $wc_countries->get_countries();
$country_states = $wc_countries->get_states();
$country_code = 'ES'; // The country code to target
printf('<p>%s <strong>%s</strong></p><ul>', __('States for '), $countries[$country_code]);
// Loop through the states array for 'GE' country
foreach( $country_states[$country_code] as $state_code => $state_name ) {
printf('<li class="state %s">%s</li>', $state_code, $state_name);
}
echo '</ul>';