Karl Johan Vallner
Karl Johan Vallner

Reputation: 4310

Get the billing state code to display the name in WooCommerce

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.

enter image description here

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

enter image description here


Edit 1.

I found something like this, but im unsure how to use it.

enter image description here

Upvotes: 3

Views: 9224

Answers (4)

LoicTheAztec
LoicTheAztec

Reputation: 254378

There are 2 ways to get that:

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>';

Upvotes: 6

Mohamed Saad
Mohamed Saad

Reputation: 415

The approved answer is not working may be cause of version changes. The Working code is that by using woocommerce customer data:

$countries_obj = new WC_Countries();
$countries_array = $countries_obj->get_countries();
$country_states_array = $countries_obj->get_states();
$user_country= get_user_meta(get_current_user_id(),'billing_country', true);
$user_state= get_user_meta(get_current_user_id(),'billing_state', true);
$onlystate= str_replace($user_country, "", $user_state);

echo $countries_array[$user_country].','.$country_states_array[$user_country][$user_state];

Upvotes: 0

Fairuz Sulaiman
Fairuz Sulaiman

Reputation: 151

here is my Code

    $user = wp_get_current_user();
    $country_name_data = get_user_meta($user->ID, 'billing_country', true);
    $state_name_data = get_user_meta($user->ID,'billing_state',true);
    
    $countries_obj = new WC_Countries();
    $countries_array = $countries_obj->get_countries();
    $country_states_array = $countries_obj->get_states();

    $country_name = $countries_array[$country_name_data];
    $state_name = $country_states_array[$country_name_data][$state_name_data];

echo "Country Name : ". $country_name ."<br/>"."State Name ":$state_name;

Upvotes: 0

Karl Johan Vallner
Karl Johan Vallner

Reputation: 4310

Well...

It seems that you have to load the countries and counties with

$wcCountries = new WC_Countries();
$wcCountries->load_country_states();

Thus the states global variable becomes available

global $states

Thus you can retrieve your full state name, by

$countyCode = isset($states[$countryCode][$countyCode]) ? $states[$countryCode][$countyCode] : $countyCode;

Since some county codes are using their full names anyway.

Happy coding!

Upvotes: 0

Related Questions