WooCommerce: Get All Shipping Zones & Rates

VIA BUSINESS BLOOMERS

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 5 * @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 5 * @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"      }      print_r( $zone_shipping_methods );		}}

WooCommerce: Get All Shipping Zones & Rates .