WC_Shipping_Zones::get_shipping_method PHP Méthode

get_shipping_method() public static méthode

Get shipping zone using it's ID
Since: 2.6.0
public static get_shipping_method ( $instance_id ) : WC_Shipping_Meethod | boolean
Résultat WC_Shipping_Meethod | boolean
    public static function get_shipping_method($instance_id)
    {
        $data_store = WC_Data_Store::load('shipping-zone');
        $raw_shipping_method = $data_store->get_method($instance_id);
        $wc_shipping = WC_Shipping::instance();
        $allowed_classes = $wc_shipping->get_shipping_method_class_names();
        if (!empty($raw_shipping_method) && in_array($raw_shipping_method->method_id, array_keys($allowed_classes))) {
            $class_name = $allowed_classes[$raw_shipping_method->method_id];
            if (is_object($class_name)) {
                $class_name = get_class($class_name);
            }
            return new $class_name($raw_shipping_method->instance_id);
        }
        return false;
    }

Usage Example

 /**
  * Test: WC_Shipping_Zones::get_shipping_method
  */
 public function test_get_shipping_method()
 {
     // Setup
     WC_Helper_Shipping_Zones::create_mock_zones();
     // Test
     $zone = WC_Shipping_Zones::get_zone_by('zone_id', 1);
     $instance_id = $zone->add_shipping_method('flat_rate');
     $shipping_method = WC_Shipping_Zones::get_shipping_method($instance_id);
     // Assert
     $this->assertInstanceOf('WC_Shipping_Flat_Rate', $shipping_method);
     // Clean
     WC_Helper_Shipping_Zones::remove_mock_zones();
 }
All Usage Examples Of WC_Shipping_Zones::get_shipping_method