Php 从WooCommerce中的国家代码获取国家名称
Wordpress中的WooCommerce对countries类的定义如下(为简洁起见编辑) 下单时,国家代码会写入Wordpress wp_Posteta表,并且可以使用Php 从WooCommerce中的国家代码获取国家名称,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,Wordpress中的WooCommerce对countries类的定义如下(为简洁起见编辑) 下单时,国家代码会写入Wordpress wp_Posteta表,并且可以使用get_post_meta()函数在任何可以访问订单id的地方提取: get_post_meta( $order->id, '_shipping_country', true ), 问题是,我们只是从数据库中提取两个字符,如何将装运国代码(如AF)转换为Countries类中给定的国家名称 您可以使用WC()->Co
get_post_meta()
函数在任何可以访问订单id的地方提取:
get_post_meta( $order->id, '_shipping_country', true ),
问题是,我们只是从数据库中提取两个字符,如何将装运国代码(如AF)转换为Countries类中给定的国家名称 您可以使用
WC()->Countries
访问WC\u 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=!空($states[$order->get_shipping_state()])$状态[$order->get_shipping_state()]:'';
从您可以使用的州代码中获取州名称
WC()->countries->states[$order->billing_country][$order->billing_state];
首先,您已经设置了产品标签“VIP”或您喜欢的任何东西,然后您将其添加到代码中
if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
$cat_check = true;
}
在该功能中,是否有任何带有“VIP”产品标签的产品。
和$cat\u check=true;
然后在这个in函数中添加函数
woo\u override\u checkout\u字段($fields)
函数设置限制
国家作为航运国字段
内部woo\u覆盖\u签出\u字段($fields){}
设置要显示的国家/地区
我正在寻找如何获取Woocommerce商店的基本地址,而不是购物车或订单地址,这条线索包含许多与主题相关的谷歌关键词。 我得到了下面的答案,我稍微更新了一下:
私有静态函数afg_getBaseAddressData(){
返回数组(
“地址”=>WC()->国家->获取基本地址(),
'address-2'=>WC()->countries->get_base_address_2(),
“城市”=>WC()->countries->get_base_city(),
'zip'=>WC()->countries->get_base_postcode(),
'state'=>WC()->countries->get_base_state(),
“国家”=>WC()->countries->countries[WC()->countries->get\u base\u country()],
'mail'=>self::afg_getPublicMail()
);
}
私有静态函数afg_getPublicMail(){
//替换为获取电子邮件地址的方式
返回filter\u var(获取选项('address-public-mail')、filter\u VALIDATE\u EMAIL);
}
公共静态函数afg_getAddressTemplate(){
$datas=apply_过滤器('afg_shop_base_address',self::afg_getBaseAddressData());
$html='';
$html.='';
foreach($datas$key=>$data){
如果($数据){
$html.='.$data.
';
}
}
$html.='';
返回$html;
}
以及如何获得特定语言的国家名称?例如sk_sk?非常好的补充。我会将此添加到已接受的答案中。
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' );
if ( has_term( 'VIP', 'product_tag', $product->id ) ) {//search product_cat
$cat_check = true;
}
$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)')
);
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;
}