This is what I'm doing, I have added 3 shipping zone at the dashboard:
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 ofWC()->customer
. InWC()->session
for protected'shipping_for_package_0'
data, you can access this data this way:
// 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_review_order_before_shipping
(for checkout)