3

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Karl Johan Vallner
  • 3,980
  • 4
  • 35
  • 46

3 Answers3

7

There is 2 ways to get that.

1) Directly using WC()->countries object (when available):

$countries = WC()->countries;

2) Using an instance of WC_Countries object:

$countries_obj = new WC_Countries();

Then you can use any WC_Countries methods.

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_array = WC()->countries->get_countries();

// Get all country states key/names in a multilevel array:
$country_states_array = WC()->countries->get_states();

OR

$countries_obj = new WC_Countries();

// Get all countries key/names in an array:
$countries_array = $countries_obj->get_countries();

// Get all country states key/names in a multilevel array:
$country_states_array = $countries_obj->get_states();

So for example if the country code is GR and the state code is D:

$countries_obj = new WC_Countries();
$countries_array = $countries_obj->get_countries();
$country_states_array = $countries_obj->get_states();

// Get the country name:
$country_name = $countries_array['GR'];

// Get the state name:
$state_name = $country_states_array['GR']['D'];

// Display names:
echo 'Country name: ' . $country_name . ' <br>State name: ' . $state_name;

This will display:

Country name: Greece
State name: Ήπειρος

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for the code above it helped me. I have a question. If I have a 40 states name that I manually added them, to get each one to echo do I have to add ($state_name = $country_states_array['GR']['D'];) for each of them with their unique "key"? – Bassam Radi May 14 '21 at 22:35
0

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!

Karl Johan Vallner
  • 3,980
  • 4
  • 35
  • 46
0

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;
Fairuz Sulaiman
  • 151
  • 1
  • 7