获取WooCommerce

问题描述:

特定的城市,州和邮编航运区这是我在做什么, 我加入了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; 

} 

你应该尝试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) –