class-wc-shipping-zones.php 4.1 KB

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