【问题标题】:Display the name of the billing and shipping country instead of the country code for the current user only [duplicate]仅显示当前用户的帐单和发货国家/地区名称,而不是国家/地区代码[重复]
【发布时间】:2020-08-13 15:55:45
【问题描述】:

灵感来自: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 
}
?>

` 我也要求它以同样的方式显示运输国家的名称。

非常感谢您能指导我。

【问题讨论】:

  • 我相信这回答了你的问题Getting country name from country code in WooCommerce。所以你得到echo WC()-&gt;countries-&gt;countries[ $custom_user_meta ];
  • 您好@7uc1f3r 不是因为该帖子是指获取订单所在的国家/地区。我需要获取当前用户的帐单和送货国家/地区的名称。
  • 好吧,你可以使用WC()-&gt;countries-&gt;countries[ $custom_user_meta ];,而不是$order-&gt;get_shipping_country()
  • @7uc1f3r 什么也没显示。您可以添加完整的代码。

标签: php wordpress woocommerce hook-woocommerce


【解决方案1】:

你可以使用WC()-&gt;countries-&gt;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 
}
?>

【讨论】:

  • 效果很好,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多