简体   繁体   English

在WooCommerce中获取特定城市,州和邮政编码的送货地区

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

This is what I'm doing, I have added 3 shipping zone at the dashboard: 这就是我正在做的,我在仪表板上添加了3个运输区域:

  1. Shipping zone 1 "2 cities" 航运区1“ 2个城市”
  2. shipping zone 2 "12 city" 航运区2“ 12城市”
  3. shipping zone 3 "rest of the world" 航运区3“世界其他地区”

And, I want to show specific messages related to the delivery process at the checkout page, but I couldn't get in which zone the customer address is. 而且,我想在结帐页面上显示与交付过程有关的特定消息,但是我无法获得客户地址所在的区域。

What I did is as follows: 我所做的如下:

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

}

I found this function but it is always returns the 3rd zone I don't know why ? 我找到了这个功能,但是它总是返回第三个区域,我不知道为什么?

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

} 

You should try WC()->session instead of WC()->customer . 您应该尝试使用WC()->session而不是WC()->customer In WC()->session for protected 'shipping_for_package_0' data, you can access this data this way: 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>';

There is also: 还有:

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

You can also use this code in a custom function hooked in this action hooks: 您还可以在此动作挂钩中挂接的自定义函数中使用以下代码:

  • woocommerce_cart_totals_before_shipping (for cart) woocommerce_cart_totals_before_shipping (用于购物车)
  • woocommerce_review_order_before_shipping (for checkout) woocommerce_review_order_before_shipping (用于结帐)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM