从WooCommerce中的国家代码获取国家名称

14

WooCommerce对国家的定义如下(已简化):

class WC_Countries {
    public $countries;

    public function __construct() {
        global $woocommerce, $states;

        $this->countries = apply_filters( 'woocommerce_countries', array(
            'AF' => __( 'Afghanistan', 'woocommerce' ),
            'AX' => __( 'Åland Islands', 'woocommerce' ),
            'AL' => __( 'Albania', 'woocommerce' ),
            'DZ' => __( 'Algeria', 'woocommerce' ),
            // […]
        ));
    }
}

下订单时,国家代码会被写入WordPress的wp_postmeta表中,并可以在使用get_post_meta()函数访问订单ID的任何位置提取:

get_post_meta( $order->id, '_shipping_country', true ),

由于我们只是从数据库中检索两个字符,所以问题在于如何将运输国家代码(例如AF)翻译成WC_Countries类中指定的国家名称?

5个回答

43
您可以使用WC()->countries访问WC_Countries类。因此,要从订单中获取国家名称,必须使用以下代码:
WC()->countries->countries[ $order->shipping_country ];

在WooCommerce 3.0+版本中,你应该使用:

WC()->countries->countries[ $order->get_shipping_country() ];

如果您想获取州名,需要先检查是否存在,因为WooCommerce并没有包含所有的州名。以下是您需要做的:

$states = WC()->countries->get_states( $order->get_shipping_country() );
$state  = ! empty( $states[ $order->get_shipping_state() ] ) ? $states[ $order->get_shipping_state() ] : '';

1
如何以特定语言获取国家名称?例如sk_SK? - Jasom Dotnet
简洁明了的解决方案,只是想提一下最好使用WC()->countries->get_countries()来获取国家数组。 - Petschko

9
要从状态代码获取状态名称,您可以使用以下方法。
WC()->countries->states[$order->billing_country][$order->billing_state];

非常好的补充。我会将其添加到被接受的答案中。 - guaranteed.site

1
global $woocommerce;
$shipping_country = WC()->customer->get_shipping_country();

0

我正在寻找如何获取Woocommerce商店基础地址,而不是购物车或订单地址,这个主题涉及许多与该主题相关的Google关键字。 我遇到了以下答案,稍作更新: https://wordpress.stackexchange.com/a/334608/177690

private static function afg_getBaseAddressData() {
    return array(
        'address' => WC()->countries->get_base_address(),
        'address-2' => WC()->countries->get_base_address_2(),
        'city' => WC()->countries->get_base_city(),
        'zip' => WC()->countries->get_base_postcode(),
        'state' => WC()->countries->get_base_state(),
        'country' => WC()->countries->countries[WC()->countries->get_base_country()],
        'mail' => self::afg_getPublicMail()
    );
}

private static function afg_getPublicMail() {
    //replace by the way you get an email address
    return filter_var(get_option('address-public-mail'), FILTER_VALIDATE_EMAIL);
}

public static function afg_getAddressTemplate() {
    $datas = apply_filters( 'afg_shop_base_address', self::afg_getBaseAddressData() );
    $html = '';

    $html .= '<address>'; 
    foreach ($datas as $key => $data) {
        if($data) {
            $html .= '<p class="address-line '.$key.'">'.$data.'</p>';
        }
    }
    $html .= '</address>';

    return $html;
}

-1
function a000_remove_bundles_counting(){
//////////////////////////////
global $woocommerce_bundles;
remove_filter( 'woocommerce_cart_contents_count',
array( $woocommerce_bundles->display, 'woo_bundles_cart_contents_count' ) );
}
add_action( 'init', 'a000_remove_bundles_counting' );
///////////////////////////////////////////////////

//////////////////////////////////////////////////////

function d000_cart_contents_count( $count ) {
    global $woocommerce;
 $cat_check = false;
  foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) { //foreach

 $product = $cart_item['data'];

 if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
$cat_check = true;
// break because we only need one "true" to matter here
if (!function_exists('woo_override_checkout_fields')) {
function woo_override_checkout_fields( $fields ) { // woo_override_checkout_fields Function


$fields['billing']['billing_country'] = array(
'type' => 'select',
'label' => __('Country', 'woocommerce'),
'options' => array('US' => 'United States(US)')
); 

$fields['billing']['billing_state'] = array(
'type' => 'select',
'label' => __('State', 'woocommerce'),
'options' => array('CA' => 'California(CA)')

);

return $fields; 
} //end woo_override_checkout_fields Function
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields' );

} // end search product_cat 
  }// end foreach



return $count;
}
add_filter( 'woocommerce_cart_contents_count',
'd000_cart_contents_count' );

首先,您需要设置产品标签“VIP”或其他您喜欢的标签,然后将其添加到代码中。

if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat 

    $cat_check = true;
}

在这个函数中,查看是否有带“VIP”产品标签的任何产品。 并且 $cat_check = true; 然后在函数内部,我们添加函数woo_override_checkout_fields($fields) 以设置限制国家为运输国家字段。
woo_override_checkout_fields($fields) { } 内部
设置您想要显示的国家。
$fields['billing']['billing_country'] = array(
'type' => 'select',
'label' => __('Country', 'woocommerce'),
'options' => array('US' => 'United States(US)')
);



$fields['billing']['billing_state'] = array(
'type' => 'select',
'label' => __('State', 'woocommerce'),
'options' => array('CA' => 'California(CA)')

);

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