How to Get Current WooCommerce Shipping Zone

WooCommerce Get Current Shipping ZoneIn this post, I want to show you how to get current WooCommerce shipping zone using a simple code snippet that we will test and prove it works and will work for you!  Before I share the WooCommerce get current shipping zone code snippet, it helps to give a quick overview about WooCommerce shipping zones.

WooCommerce Get Current Shipping Zone

First, you need to ensure the shipping zones are set up in the WooCommerce settings page as shown on the image below:WooCommerce Get Current Shipping Zone

When the shipping zone is already set we can now go ahead and test our code snippet we see how it works!

How to Get Current Shipping Zone in WooCommerce

We can get the current shipping zone by using the WC Shipping Zone class and getting the package that contains all the details of the shipping zone in WooCommerce.

The following is a simple code snippet that we need to get the current shipping zone object and the second line to extract the current shipping zone name:

[php]

$current_shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );

$zone_name=$shipping_zone->get_zone_name();

[/php]

We can now add this snippet to our test site to see how it works.

Get Current WooCommerce Shipping Zone and Display on Header 

For demonstration, I will add this code snippet to the head action hook so that we can test and see how you can easily get the shipping zone.

The following is the full code snippet with the code above inside the action hook to the header to display the current WooCommerce Shipping zone:

[php]

//WooCommerce Get Current Shipping Zone

add_action(‘wp_head’, ‘get_current_shipping_zone’);

function get_current_shipping_zone(){

$current_shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );

$current_shipping_zone_name= $current_shipping_zone ->get_zone_name();

print(‘<pre>’);

print_r( $current_shipping_zone_name);

print(‘</pre>’);

}

[/php]

When you add this code to the test theme you should see the shipping zone is displayed on the header as shown on the image below:WooCommerce Get Current Shipping Zone

Conclusion

In this post, I have outlined how you can get the current WooCommerce shipping zone using the code snippet I shared.

The most important take from this tutorial is how to make use of the WC Shipping Zone class that helps us get the current WooCommerce shipping zones using the $package parameter.

Similar Articles