I need to calculate how much is it till free shipping. I have two shipping zones and I need to know which is current. I've only managed to get all shipping zones that are defined but cannot find anything to get me the information about current shipping zone. That has to be changed accordingly to shipping calculator.
Is there a way to get current shipping zone?
function action_woocommerce_cart_totals_after_order_total() {
echo 'Bis kostenloser versand'; // till free shipping
$IsValue = WC()->cart->get_subtotal();
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone ) {
echo $the_zone['zone_name'];
// I need to know which is current
}
};
add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_cart_totals_after_order_total', 10, 0 );
To get "Get current shipping zone" you can use get_zone_matching_package()
for that you have to pass current package that you can get by this WC()->session->get( 'chosen_shipping_methods' )[0]
. try below code.
function action_woocommerce_cart_totals_after_order_total() {
$shipping_id = WC()->session->get( 'chosen_shipping_methods' )[0];
$packages = WC()->shipping()->get_packages();
foreach ( $packages as $i => $package ) {
if ( isset( $package['rates'] ) && isset( $package['rates'][ $shipping_id ] ) ) {
$package = $package;
break;
}
}
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$zone = $shipping_zone->get_zone_name();
echo "zone => ".$zone;
};
add_action( 'woocommerce_cart_totals_after_order_total', 'action_woocommerce_cart_totals_after_order_total', 10, 0 );