获取账单状态代码以在 WooCommerce 中显示名称

Get the billing state code to display the name in WooCommerce

我拼命想了解,WooCommerce 如何将某些国家/地区 county/state 名称转换为数据库字段常量并返回。

我有一位来自希腊的客户,他恰好来自 county/state,我没有这个键盘上的字母可以命名。

显然,即使 WooCommerce 也没有对应的字母,因为在数据库中,county/state 也仅保存为 "D"。

我可以使用什么函数将其还原为

的前端形式


编辑 1.

我找到了类似的东西,但我不确定如何使用它。

嗯...

看来要加载国家和县

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

因此 states 全局变量可用

global $states

因此您可以通过

检索您的州全名
$countyCode = isset($states[$countryCode][$countyCode]) ? $states[$countryCode][$countyCode] : $countyCode;

因为一些县代码无论如何都使用他们的全名。

编码愉快!

There is 2 ways to get that.

1) 直接使用 WC()->countries 对象(可用时):

$countries = WC()->countries;

2) 使用 WC_Countries 对象的实例:

$countries_obj = new WC_Countries();

Then you can use any WC_Countries methods.

获取数组中的所有运输国家代码/名称并获取多级数组中的所有国家/地区代码/名称:

// 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();

$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();

例如,如果国家/地区代码是 GR 并且州代码是 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;

这将显示:

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