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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

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'];
      }
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

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. Follow @rmelogli

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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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