[英]Get the shipping zone for specific city, state and postal code in WooCommerce
这就是我正在做的,我在仪表板上添加了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.