woocommerce 中似乎有一个小错误。
那么由于它是与订单相关的电子邮件通知,您应该使用 get_billing_country() 方法和 WC_Order 对象。
对于 WC_Order 对象的 get_billing_country() 方法,这可以通过下面的转向来解决:
add_filter( 'woocommerce_order_get_billing_country','hook_order_get_billing_country', 10, 2);
function hook_order_get_billing_country( $value, $order ){
// Country codes/names
$countries_obj = new WC_Countries;
$country_array = $countries_obj->get_countries();
// Get the country name from the country code
$country_name = $country_array[ $value ];
// If country name is not empty we output it
if( $country_name )
return $country_name;
// If $value argument is empty we get the country code
if( empty( $value ) ){
$country_code = get_post_meta( $order->get_id(), '_billing_country', true );
// We output the country name
return $country_array[ $country_code ];
}
return $value;
}
代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。
代码经过测试,适用于 wooCommerce 版本 3+
所以现在你应该让它工作并输出正确的国家/地区名称
对于 get_billing_country() 方法的 WC_Customer 对象,您应该使用这个:
add_filter( 'woocommerce_customer_get_billing_country','wc_customer_get_billing_country', 10, 2);
function wc_customer_get_billing_country( $value, $customer ){
// Country codes/names
$countries_obj = new WC_Countries;
$country_array = $countries_obj->get_countries();
// Get the country name from the country code
$country_name = $country_array[ $value ];
// If country name is not empty we output it
if( $country_name )
return $country_name;
// If $value argument is empty we get the country code
if( empty( $value ) ){
$country_code = get_user_meta( $customer->get_id(), 'billing_country', true );
// We output the country name
return $country_array[ $country_code ];
}
return $value;
}
代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。