获取状态名称而不是 Woocommerce 中的代码 [英] Get state name instead of the code in Woocommerce

查看:31
本文介绍了获取状态名称而不是 Woocommerce 中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have added a custom list of states to my woocommerce using the code here: https://docs.woocommerce.com/document/addmodify-states/

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

The newly added states load fine on frontend and some backend screens, however in e-mails and user account screen, woocommerce only loads the the code / value instead of the actual name. (XX1, XX2 etc.)

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

I believe I can fix it using this logic:

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

所以我想知道是否可以使用这个逻辑来创建一个函数,每当模板调用代码时都会打印状态名称?任何帮助将不胜感激.

So I was wondering if it's possible to use this logic to create a function that would print the state name whenever the template calls the code? Any help would be very much appreciated.

推荐答案

您可以使用以下从当前用户 (WP_User object) 获取州标签名称:

You can use the following from the current user (WP_User object) to get the state label name:

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

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

经过测试并有效

对于来自订单 ID 的订单:

And for an order from the Order 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];

这篇关于获取状态名称而不是 Woocommerce 中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆