簡體   English   中英

在WooCommerce中獲取特定城市,州和郵政編碼的送貨地區

[英]Get the shipping zone for specific city, state and postal code in WooCommerce

這就是我正在做的,我在儀表板上添加了3個運輸區域:

  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 ... ? 

}

我找到了這個功能,但是它總是返回第三個區域,我不知道為什么?

/* 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;

} 

您應該嘗試使用WC()->session而不是WC()->customer WC()->session中受保護的'shipping_for_package_0'數據中,您可以通過以下方式訪問此數據:

// 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');

您還可以在此動作掛鈎中掛接的自定義函數中使用以下代碼:

  • woocommerce_cart_totals_before_shipping (用於購物車)
  • woocommerce_review_order_before_shipping (用於結帳)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM