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

Display the full country name rather than country code in WooCommerce?

尝试使用 <?php echo $current_user->billing_country; ?> 显示 WooCommerce 用户账单国家。例如,如果客户的账单地址为澳大利亚,则回显为 AU。但是,我想用全名代替国家代码。

billing_state也是如此。我没有得到南澳大利亚,而是得到了 SA。已通过 functions.php 完成工作,但前提是我添加了每个州和国家/地区。这显然是一项将世界各国映射到国家代码的伟大事业。

我相信我以前用一种非常简单的方式做到了这一点,但出于某种原因现在无法弄清楚。

在禁用输入字段中使用国家和州作为值

<input type="text" class="form-control" value="<?php echo $current_user->billing_country; ?>" disabled="">

我们目前得到的图片

我们想要得到的图像

WooCommerce 有 WC_Countries class 为此,您可以这样使用它:

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

希望对您有所帮助!