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

标签 php wordpress woocommerce states countries

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

即。

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

enter image description here

显然,即使 WooCommerce 也没有字母,因为在数据库中,县/州也仅保存为“D”。

我可以使用什么函数将其还原为它的前端形式

enter image description here


编辑1.

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

enter image description here

最佳答案

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

关于php - 获取账单状态代码以在 WooCommerce 中显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45665811/

相关文章:

php - 多个数据库或内容较多的表

php - Yii2 - 在 GridView 中按一对多对一关系排序和过滤

php - 如何在 php 中正确实现结构化菜单

php - WooCommerce:将自定义元添加为隐藏的订单项元以供内部使用

wordpress - 显示描述而不是摘录,限制字数并添加阅读更多

php - 试图获取非对象 Laravel 5.4 的属性

PhpStorm:满足前提条件时如何处理未处理的异常

php - WordPress 按数组中的元值排序

css - 无法从一个菜单项中删除 Webkit-box 阴影

wordpress - 我想使用 woocommerce 将购物车中的产品总数限制为 3