php 从 WooCommerce 中的国家/地区代码获取国家/地区名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25528454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting country name from country code in WooCommerce
提问by Christian Mayne
WooCommerce in Wordpress defines the countries class as follows (edited fro brevity)
Wordpress 中的 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' ),
));
}
}
When an order is placed, the country code is written to the Wordpress wp_postmeta table and can be extracted anywhere an order id can be accessed using the get_post_meta()
function:
下订单时,国家/地区代码将写入 Wordpress wp_postmeta 表,并且可以在任何可以使用以下get_post_meta()
函数访问订单 ID 的地方提取:
get_post_meta( $order->id, '_shipping_country', true ),
The question is, as weare simply pulling two characters from the DB, how can the Shipping country code (eg AF) be translated to the Country Name given in the Countries class?
问题是,由于只需从数据库中提取两个字符,如何将运输国家/地区代码(例如 AF)转换为国家类中给出的国家/地区名称?
回答by Claudio Sanches
You can access the WC_Countries
class with WC()->countries
. So to get the country name from an order, you must use:
您可以使用 访问WC_Countries
该类WC()->countries
。因此,要从订单中获取国家/地区名称,您必须使用:
WC()->countries->countries[ $order->shipping_country ];
On WooCommerce 3.0+ you should use:
在 WooCommerce 3.0+ 上,您应该使用:
WC()->countries->countries[ $order->get_shipping_country() ];
If you like to get the state, you need before check if exist, since WooCommerce doesn't include all states, so here what do you need:
如果你想获得状态,你需要在检查是否存在之前,因为 WooCommerce 不包括所有状态,所以这里你需要什么:
$states = WC()->countries->get_states( $order->get_shipping_country() );
$state = ! empty( $states[ $order->get_shipping_state() ] ) ? $states[ $order->get_shipping_state() ] : '';
回答by Ashish Khatri
To get the state name from state code you can use.
要从州代码中获取州名,您可以使用。
WC()->countries->states[$order->billing_country][$order->billing_state];
回答by Cédric Moris Kelly
I was looking how to get the Woocommerce shop base address, not the cart or an order address, and this thread comes in many Google keywords related to the topic. I came accross the following answer which I updated a little : https://wordpress.stackexchange.com/a/334608/177690
我正在寻找如何获取 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;
}
回答by T.K Dilan Kumara
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' );
First you have set product tag "VIP or what ever you like and then you will add it to code
首先,您设置了产品标签“VIP 或您喜欢的任何东西,然后将其添加到代码中”
if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
$cat_check = true;
}
in this function looking is there any product with "VIP" Product tag. and
$cat_check = true;
then inside this in function we adding function
woo_override_checkout_fields( $fields )
function to set limited country as shipping country field.Inside
woo_override_checkout_fields( $fields ) { }
Set which country you want to show
在此功能中查找是否有任何带有“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)')
);