在WooCommerce中显示完整的国家名称而不是国家代码?

7
尝试使用<?php echo $current_user->billing_country; ?>来显示WooCommerce用户的账单国家。例如,如果客户有澳大利亚的账单地址,则会输出AU。然而,我想要获取完整名称,而不是国家代码。
对于账单州,情况也一样。我得到的是SA,而不是南澳大利亚。我已经在functions.php文件中进行了处理,但必须逐个添加每个州和国家。显然,这是一个很大的工程,需要将世界各地的州与状态代码进行映射。
我相信我以前曾以非常简单的方式完成过这个任务,但现在无法理解原因。
国家和州正在被用作禁用输入字段的值。
<input type="text" class="form-control" value="<?php echo $current_user->billing_country; ?>" disabled="">

当前我们得到的图像

当前我们得到的图像

我们想要得到的图像 我们想要得到的图像

1个回答

19

WooCommerce中有WC_Countries类,你可以像这样使用它:

echo WC()->countries->countries['AU']; //To get country name by code
echo WC()->countries->states['AU']['SA']; //to get State name by state code

或者(在你的情况下

echo WC()->countries->countries[$current_user->billing_country]; //To get country name by code
echo WC()->countries->states[$current_user->billing_country][$current_user->billing_state]; //to get State name by state code
希望这可以帮到你!

1
非常感谢,运行得非常好! - Darren
@DarrenMathers:别忘了给我的答案点赞,因为这有助于你! - Raunak Gupta
1
如果状态尚未加载,请尝试使用 WC()->countries->get_states( $current_user->billing_country )[ $current_user->billing_state ] - Marian
1
这帮了很大的忙。非常感谢,先生。 - Desper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接