2017-06-18 16 views
2

特定的城市,州和郵編航運區這是我在做什麼, 我加入了3航運區,在儀表板:獲取WooCommerce

  1. 航運區1「2個城市」
  2. 航運區2「12城」
  3. 航運區

而且,我想顯示在結帳頁面關於傳送過程中的具體消息,但我不能3「世界其他地方」得到客戶地址在哪個區域。

我所做的是:

/* get the order shipping zone meta data */ 

function get_shipping_zone(){ 

    global $woocommerce; 
    $customer = new WC_Customer(); 
    $post_code = $woocommerce->customer->get_shipping_postcode(); 
    $zone_postcode = $woocommerce->customer->get_shipping_postcode(); 
    $zone_city =$woocommerce->customer->get_shipping_city(); 
    $zone_state = $woocommerce->customer->get_shipping_state(); 

    // for debugging 
    echo "<pre>"; 
    print_r($woocommerce->customer); 
    echo "</pre>"; 

    //show the customer order postal code, city 
    echo "The post code is ". $post_code." <br/>"; 

    # here I should add the code to return the customer shipping zone ... ? 

} 

,我發現這個功能,但它總是返回我不知道爲什麼第3區?

/* getting the shipping zone based on spesific package */ 

function get_shipping_zone($package=Array()) { 
    global $woocommerce; 

    $shipping_zone = WC_Shipping_Zones::get_zone_matching_package($package); 

    $zone=$shipping_zone->get_zone_name(); 

    return $zone; 

} 

回答

1

你應該嘗試WC()->session代替WC()->customer

// Accessing to 'shipping_for_package_0' protected data 
$shipping_package = WC()->session->get('shipping_for_package_0'); 

// Getting the instance of WC_Shipping_Rate object 
foreach ($shipping_package['rates'] as $shipping_rate) break; 

// Displaying accessing to the data in the object 
echo 'Rate ID: ' . $shipping_rate->id . '<br>'; 
echo 'Rate Label: ' . $shipping_rate->label . '<br>'; 
echo 'Rate Cost: ' . $shipping_rate->cost . '<br>'; 
echo 'Rate Tax: ' . $shipping_rate->taxes[1] . '<br>'; 
echo 'Method ID: ' . $shipping_rate->method_id . '<br>'; 

還有:

$chosen_shipping_method = WC()->session->get('chosen_shipping_methods'); 

您也可以在此掛鉤的自定義功能使用此代碼在WC()->session爲保護'shipping_for_package_0'數據,你可以訪問此數據這樣動作掛鉤:

  • woocommerce_cart_totals_before_shipping(推車)
  • woocommerce_review_order_before_shipping(結賬)
+0

謝謝盧瓦克的回答,我不知道該WC() - 之前>會話。高度讚賞。 –

+0

高度讚賞,重拳出擊(y) –