WC_Shipping_Zones

Handles storage and retrieval of shipping zones.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-shipping-zones.php  
  1. class WC_Shipping_Zones { 
  2.  
  3. /** 
  4. * Get shipping zones from the database 
  5. * @since 2.6.0 
  6. * @return array of arrays 
  7. */ 
  8. public static function get_zones() { 
  9. $data_store = WC_Data_Store::load( 'shipping-zone' ); 
  10. $raw_zones = $data_store->get_zones(); 
  11. $zones = array(); 
  12.  
  13. foreach ( $raw_zones as $raw_zone ) { 
  14. $zone = new WC_Shipping_Zone( $raw_zone ); 
  15. $zones[ $zone->get_id() ] = $zone->get_data(); 
  16. $zones[ $zone->get_id() ]['zone_id'] = $zone->get_id(); 
  17. $zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location(); 
  18. $zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods(); 
  19.  
  20. return $zones; 
  21.  
  22. /** 
  23. * Get shipping zone using it's ID 
  24. * @since 2.6.0 
  25. * @param int $zone_id 
  26. * @return WC_Shipping_Zone|bool 
  27. */ 
  28. public static function get_zone( $zone_id ) { 
  29. return self::get_zone_by( 'zone_id', $zone_id ); 
  30.  
  31. /** 
  32. * Get shipping zone by an ID. 
  33. * @since 2.6.0 
  34. * @param string $by zone_id or instance_id 
  35. * @param int $id 
  36. * @return WC_Shipping_Zone|bool 
  37. */ 
  38. public static function get_zone_by( $by = 'zone_id', $id = 0 ) { 
  39. switch ( $by ) { 
  40. case 'zone_id' : 
  41. $zone_id = $id; 
  42. break; 
  43. case 'instance_id' : 
  44. $data_store = WC_Data_Store::load( 'shipping-zone' ); 
  45. $zone_id = $data_store->get_zone_id_by_instance_id( $id ); 
  46. break; 
  47.  
  48. if ( false !== $zone_id ) { 
  49. try { 
  50. return new WC_Shipping_Zone( $zone_id ); 
  51. } catch ( Exception $e ) { 
  52. return false; 
  53.  
  54. return false; 
  55.  
  56. /** 
  57. * Get shipping zone using it's ID 
  58. * @since 2.6.0 
  59. * @return WC_Shipping_Meethod|bool 
  60. */ 
  61. public static function get_shipping_method( $instance_id ) { 
  62. $data_store = WC_Data_Store::load( 'shipping-zone' ); 
  63. $raw_shipping_method = $data_store->get_method( $instance_id ); 
  64. $wc_shipping = WC_Shipping::instance(); 
  65. $allowed_classes = $wc_shipping->get_shipping_method_class_names(); 
  66.  
  67. if ( ! empty( $raw_shipping_method ) && in_array( $raw_shipping_method->method_id, array_keys( $allowed_classes ) ) ) { 
  68. $class_name = $allowed_classes[ $raw_shipping_method->method_id ]; 
  69. if ( is_object( $class_name ) ) { 
  70. $class_name = get_class( $class_name ); 
  71. return new $class_name( $raw_shipping_method->instance_id ); 
  72. return false; 
  73.  
  74. /** 
  75. * Delete a zone using it's ID 
  76. * @param int $zone_id 
  77. * @since 2.6.0 
  78. */ 
  79. public static function delete_zone( $zone_id ) { 
  80. $zone = new WC_Shipping_Zone( $zone_id ); 
  81. $zone->delete(); 
  82.  
  83. /** 
  84. * Find a matching zone for a given package. 
  85. * @since 2.6.0 
  86. * @uses wc_make_numeric_postcode() 
  87. * @param object $package 
  88. * @return WC_Shipping_Zone 
  89. */ 
  90. public static function get_zone_matching_package( $package ) { 
  91. $country = strtoupper( wc_clean( $package['destination']['country'] ) ); 
  92. $state = strtoupper( wc_clean( $package['destination']['state'] ) ); 
  93. $continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) ); 
  94. $postcode = wc_normalize_postcode( wc_clean( $package['destination']['postcode'] ) ); 
  95. $cache_key = WC_Cache_Helper::get_cache_prefix( 'shipping_zones' ) . 'wc_shipping_zone_' . md5( sprintf( '%s+%s+%s', $country, $state, $postcode ) ); 
  96. $matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' ); 
  97.  
  98. if ( false === $matching_zone_id ) { 
  99. $data_store = WC_Data_Store::load( 'shipping-zone' ); 
  100. $matching_zone_id = $data_store->get_zone_id_from_package( $package ); 
  101. wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' ); 
  102.  
  103. return new WC_Shipping_Zone( $matching_zone_id ? $matching_zone_id : 0 );