WC_Shipping_Zones::get_zone_by()public staticWC 2.6.0

Get shipping zone by an ID.

Method of the class: WC_Shipping_Zones{}

No Hooks.

Return

WC_Shipping_Zone|true|false.

Usage

$result = WC_Shipping_Zones::get_zone_by( $by, $id );
$by(string)
Get by 'zone_id' or 'instance_id'.
Default: 'zone_id'
$id(int)
ID.

Changelog

Since 2.6.0 Introduced.

WC_Shipping_Zones::get_zone_by() code WC 8.6.1

public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
	$zone_id = false;

	switch ( $by ) {
		case 'zone_id':
			$zone_id = $id;
			break;
		case 'instance_id':
			$data_store = WC_Data_Store::load( 'shipping-zone' );
			$zone_id    = $data_store->get_zone_id_by_instance_id( $id );
			break;
	}

	if ( false !== $zone_id ) {
		try {
			return new WC_Shipping_Zone( $zone_id );
		} catch ( Exception $e ) {
			return false;
		}
	}

	return false;
}