WooCommerce: Get All Shipping Zones & Rates

As an advanced WooCommerce developer, at some stage you’re really going to need this PHP function, the same way you often browse through Business Bloomer’s WooCommerce visual hook guides or the product / order / cart getters.

This time we go study how to “get” the shipping zones and rates, because it’s likely that you will need to loop through them when you need to display shipping rates somewhere, or for other custom functionalities. Enjoy!

PHP Custom Function: Get All WooCommerce Shipping Zones

/**
 * @function      Get WooCommerce Shipping Zones
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

function bbloomer_get_all_shipping_zones() {
	$data_store = WC_Data_Store::load( 'shipping-zone' );
	$raw_zones = $data_store->get_zones();
	foreach ( $raw_zones as $raw_zone ) {
		$zones[] = new WC_Shipping_Zone( $raw_zone );
	}
	$zones[] = new WC_Shipping_Zone( 0 ); // ADD ZONE "0" MANUALLY
	return $zones;
}

You can then loop through the zones anywhere you need and access zone information:

foreach ( bbloomer_get_all_shipping_zones() as $zone ) {
	$zone_id = $zone->get_id();
	$zone_name = $zone->get_zone_name();
	$zone_order = $zone->get_zone_order();
	$zone_locations = $zone->get_zone_locations();
	$zone_formatted_location = $zone->get_formatted_location();
	$zone_shipping_methods = $zone->get_shipping_methods(); // SEE BELOW
}

PHP Custom Function: Get All WooCommerce Shipping Rates

Once you have access to all shipping zones, you can also loop through each of their shipping methods.

/**
 * @function      Get All WooCommerce Shipping Rates
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

function bbloomer_get_all_shipping_rates() {
   foreach ( bbloomer_get_all_shipping_zones() as $zone ) {
	   $zone_shipping_methods = $zone->get_shipping_methods();
      foreach ( $zone_shipping_methods as $index => $method ) {
		   $method_is_taxable = $method->is_taxable();
		   $method_is_enabled = $method->is_enabled();
		   $method_instance_id = $method->get_instance_id();
		   $method_title = $method->get_method_title(); // e.g. "Flat Rate"
		   $method_description = $method->get_method_description();
		   $method_user_title = $method->get_title(); // e.g. whatever you renamed "Flat Rate" into
		   $method_rate_id = $method->get_rate_id(); // e.g. "flat_rate:18"
         $instance = $method->instance_settings;
         $cost = $instance['cost'];
      }
	}
}
Was this article helpful?
YesNo

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

2 thoughts on “WooCommerce: Get All Shipping Zones & Rates

  1. Did you forget to add an add_action?

    1. Thanks for your Q! Nope, this was not meant to work with an action, I simply created a custom PHP function. You can then use it inside your code (including a function triggered by an action), e.g.:

      add_action( 'whatever', 'trigger_this' );
      
      function trigger_this() {
         print_r( bbloomer_get_all_shipping_zones() );
      }
      

      Hope this helps

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *