Woocommerce get selected shipping zone id for the current user

The problem is that WC_Cart get_shipping_packages() method gives an array of shipping packages and the function wc_get_shipping_zone( $package ) expect to have as argument a unique package.

So you need to get only one package, using for example php reset() function like:

// Get cart shipping packages
$shipping_packages =  WC()->cart->get_shipping_packages();

// Get the WC_Shipping_Zones instance object for the first package
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );

$zone_id   = $shipping_zone->get_id(); // Get the zone ID
$zone_name = $shipping_zone->get_zone_name(); // Get the zone name

// Testing output
echo '<p>Zone id: ' . $zone_id . ' | Zone name: ' . $zone_name . '</p>';

Tested and works.

Leave a Comment