显示开票和运送国家的名称,而不是仅显示当前用户的国家/地区代码

问题描述

灵感来自:Get state name instead of the code in Woocommerce Display a preview of the custom field data in the Edit Billing and Shipping Address section on the my account page并且在朋友@LoicTheAztec的回答中,我打算在(编辑帐单和送货地址)部分中显示国家名称 > 页面上的我的帐户,其中包含以下代码,但仅显示国家代码而不是完整的国家名称

<?PHP
    $custom_user_Meta = get_user_Meta(get_current_user_id(),'billing_country',true);

    if( ! empty($custom_user_Meta) )
    { ?>
<p> <?PHP
printf( '<strong>Country / Region:</strong> ' . esc_html( '%s' ),esc_html($custom_user_Meta)  );?> 
</p> <?PHP 
    }
    ?>

` 我还要求以同样的方式显示发货国的名称

非常感谢您能为我提供指导。

解决方法

您可以使用WC()->countries->countries[ 'COUNTRY CODE' ];

<?php
$custom_user_meta = get_user_meta(get_current_user_id(),'billing_country',true);

if( ! empty($custom_user_meta) ) {
?>
    <p>
    <?php
    // Get country name
    $country_name = WC()->countries->countries[ $custom_user_meta ];
    
    printf( '<strong>Country / Region:</strong> ' . esc_html( '%s' ),esc_html($country_name) );
    ?> 
    </p>
    <?php 
}
?>