WC_Shipping_Zones::get_zone_by PHP Method

get_zone_by() public static method

Get shipping zone by an ID.
Since: 2.6.0
public static get_zone_by ( string $by = 'zone_id', integer $id ) : WC_Shipping_Zone | boolean
$by string zone_id or instance_id
$id integer
return WC_Shipping_Zone | boolean
    public static function get_zone_by($by = 'zone_id', $id = 0)
    {
        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;
    }

Usage Example

 /**
  * Retrieve a Shipping Zone by it's ID.
  *
  * @param int $zone_id Shipping Zone ID.
  * @return WC_Shipping_Zone|WP_Error
  */
 protected function get_zone($zone_id)
 {
     $zone = WC_Shipping_Zones::get_zone_by('zone_id', $zone_id);
     if (false === $zone) {
         return new WP_Error('woocommerce_rest_shipping_zone_invalid', __("Resource doesn't exist.", 'woocommerce'), array('status' => 404));
     }
     return $zone;
 }
All Usage Examples Of WC_Shipping_Zones::get_zone_by