获取状态名称而不是 Woocommerce 中的代码

Get state name instead of the code in Woocommerce

我已经使用此处的代码向我的 woocommerce 添加了自定义状态列表:https://docs.woocommerce.com/document/addmodify-states/

新添加的状态在前端和一些后端屏幕上加载正常,但是在电子邮件和用户帐户屏幕上,woocommerce 仅加载代码/值而不是实际名称。 (XX1、XX2 等)

我相信我可以使用以下逻辑修复它:

echo WC()->countries->states[$current_user->billing_country][$current_user->billing_state]; //to get State name by state code

所以我想知道是否可以使用此逻辑来创建一个函数,以便在模板调用代码时打印状态名称?非常感谢任何帮助。

您可以使用当前用户 WP_User 对象) 中的以下内容来获取 状态标签名称

$user = wp_get_current_user(); // The current user

$country = $user->billing_country;
$state = $user->billing_state;
echo WC()->countries->get_states( $country )[$state];

已测试并有效


对于来自订单 ID 的订单:

$order = wc_get_order($order_id); // The WC_Order object

$country = $order->get_billing_country();
$state = $order->get_billing_state();
echo WC()->countries->get_states( $country )[$state];