在WooCommerce

时间:2017-06-18 18:16:47

标签: php wordpress woocommerce shipping orders

这就是我正在做的事情, 我在仪表板上添加了3个运输区域:

  1. 送货区1“2个城市”
  2. 航运区2“12 city”
  3. 航运区3“世界其他地方”
  4. 并且,我想在结帐页面上显示与交付流程相关的特定消息,但我无法获得客户地址所在的区域。

    我所做的如下:

    /* 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 个答案:

答案 0 :(得分:3)

  

您应该尝试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 (结帐)
  •