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: Ήπειρος