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
 * @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        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @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

  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]
  • WooCommerce: Hide Shipping Rates if Free Shipping Available
    If Free Shipping is available, you possibly don’t want to show the other premium shipping options. WooCommerce shows by default all shipping rates that match a given shipping zone, so it’s not possible to achieve this from the settings alone. Thankfully, the “woocommerce_package_rates” filter allows us to manipulate the shipping rates before they are returned […]
  • WooCommerce: Hide Shipping If Local Pickup Is Selected
    Let’s talk about checkout UX: if a user is willing to pick up the item in store, why should there be a shipping form on the checkout? Well, let’s see how we can hide this dynamically with a bit of PHP and JS!

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 *