2014-08-27 36 views
4

WooCommerce在WordPress的定義國家類,如下所示(編輯來回簡潔)獲得國名

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表,並且可以可以隨時隨地提取訂單ID可以使用get_post_meta()函數訪問:

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

的問題是,作爲WEARE簡單地從數據庫中拉兩個人物,怎麼能送貨的國家代碼(例如AF)被翻譯成國家名字在中給出國家班?

回答

13

您可以通過WC()->countries訪問WC_Countries類。所以從訂單獲得國家名稱,則必須使用:

WC()->countries->countries[ $order->shipping_country ]; 
+0

以及如何獲得特定語言的國家名稱?例如sk_SK? – 2017-06-01 09:45:04

2

爲了充分利用可使用狀態代碼狀態名稱。

WC()->countries->states[$order->billing_country][$order->billing_state]; 
-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)') 

); 
+0

這是發佈在錯誤的問題上嗎? – 2017-06-08 22:24:40