2015-12-10 11:55:03 +00:00
|
|
|
<?php
|
2017-12-06 21:11:40 +00:00
|
|
|
/**
|
|
|
|
* Handles storage and retrieval of shipping zones
|
|
|
|
*
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @version 3.3.0
|
2017-12-06 21:12:42 +00:00
|
|
|
* @since 2.6.0
|
2017-12-06 21:11:40 +00:00
|
|
|
*/
|
2015-12-10 11:55:03 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-06 21:11:40 +00:00
|
|
|
* Shipping zones class.
|
2015-12-10 11:55:03 +00:00
|
|
|
*/
|
|
|
|
class WC_Shipping_Zones {
|
|
|
|
|
|
|
|
/**
|
2017-12-06 21:11:40 +00:00
|
|
|
* Get shipping zones from the database.
|
|
|
|
*
|
2015-12-11 14:11:12 +00:00
|
|
|
* @since 2.6.0
|
2018-05-09 14:35:49 +00:00
|
|
|
* @param string $context Getting shipping methods for what context. Valid values, admin, json.
|
2017-12-06 21:11:40 +00:00
|
|
|
* @return array Array of arrays.
|
2015-12-10 11:55:03 +00:00
|
|
|
*/
|
2018-05-09 14:35:49 +00:00
|
|
|
public static function get_zones( $context = 'admin' ) {
|
2016-11-15 22:55:14 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
|
|
|
$raw_zones = $data_store->get_zones();
|
|
|
|
$zones = array();
|
2015-12-10 11:55:03 +00:00
|
|
|
|
|
|
|
foreach ( $raw_zones as $raw_zone ) {
|
2019-12-20 17:21:08 +00:00
|
|
|
$zone = new WC_Shipping_Zone( $raw_zone );
|
|
|
|
$zones[ $zone->get_id() ] = $zone->get_data();
|
|
|
|
$zones[ $zone->get_id() ]['zone_id'] = $zone->get_id();
|
2016-11-15 22:55:14 +00:00
|
|
|
$zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
|
2018-05-09 14:35:49 +00:00
|
|
|
$zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods( false, $context );
|
2015-12-10 11:55:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-16 13:28:55 +00:00
|
|
|
return $zones;
|
2016-04-22 14:45:34 +00:00
|
|
|
}
|
2015-12-10 11:55:03 +00:00
|
|
|
|
2015-12-11 14:11:12 +00:00
|
|
|
/**
|
|
|
|
* Get shipping zone using it's ID
|
2017-12-06 21:11:40 +00:00
|
|
|
*
|
2015-12-11 14:11:12 +00:00
|
|
|
* @since 2.6.0
|
2017-12-06 21:11:40 +00:00
|
|
|
* @param int $zone_id Zone ID.
|
2015-12-11 14:11:12 +00:00
|
|
|
* @return WC_Shipping_Zone|bool
|
|
|
|
*/
|
|
|
|
public static function get_zone( $zone_id ) {
|
|
|
|
return self::get_zone_by( 'zone_id', $zone_id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get shipping zone by an ID.
|
2017-12-06 21:11:40 +00:00
|
|
|
*
|
2015-12-11 14:11:12 +00:00
|
|
|
* @since 2.6.0
|
2017-12-06 21:11:40 +00:00
|
|
|
* @param string $by Get by 'zone_id' or 'instance_id'.
|
|
|
|
* @param int $id ID.
|
2015-12-11 14:11:12 +00:00
|
|
|
* @return WC_Shipping_Zone|bool
|
|
|
|
*/
|
|
|
|
public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
|
2017-12-06 21:07:00 +00:00
|
|
|
$zone_id = false;
|
|
|
|
|
2015-12-11 14:11:12 +00:00
|
|
|
switch ( $by ) {
|
2017-12-06 21:11:40 +00:00
|
|
|
case 'zone_id':
|
2016-11-15 23:16:04 +00:00
|
|
|
$zone_id = $id;
|
2017-12-06 21:11:40 +00:00
|
|
|
break;
|
|
|
|
case 'instance_id':
|
2016-11-15 22:55:14 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
|
|
|
$zone_id = $data_store->get_zone_id_by_instance_id( $id );
|
2017-12-06 21:11:40 +00:00
|
|
|
break;
|
2015-12-11 14:11:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 23:16:04 +00:00
|
|
|
if ( false !== $zone_id ) {
|
|
|
|
try {
|
|
|
|
return new WC_Shipping_Zone( $zone_id );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-15 21:53:11 +00:00
|
|
|
return false;
|
2015-12-11 14:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-06 21:11:40 +00:00
|
|
|
* Get shipping zone using it's ID.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2017-12-06 21:11:40 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @param int $instance_id Instance ID.
|
2017-07-17 10:10:52 +00:00
|
|
|
* @return bool|WC_Shipping_Method
|
2015-12-11 14:11:12 +00:00
|
|
|
*/
|
|
|
|
public static function get_shipping_method( $instance_id ) {
|
2016-11-15 22:55:14 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
|
|
|
$raw_shipping_method = $data_store->get_method( $instance_id );
|
2015-12-11 14:11:12 +00:00
|
|
|
$wc_shipping = WC_Shipping::instance();
|
|
|
|
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
|
|
|
|
2018-03-14 20:53:41 +00:00
|
|
|
if ( ! empty( $raw_shipping_method ) && in_array( $raw_shipping_method->method_id, array_keys( $allowed_classes ), true ) ) {
|
2015-12-11 14:11:12 +00:00
|
|
|
$class_name = $allowed_classes[ $raw_shipping_method->method_id ];
|
2016-02-25 19:56:05 +00:00
|
|
|
if ( is_object( $class_name ) ) {
|
|
|
|
$class_name = get_class( $class_name );
|
|
|
|
}
|
2015-12-11 14:11:12 +00:00
|
|
|
return new $class_name( $raw_shipping_method->instance_id );
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a zone using it's ID
|
2017-12-06 21:11:40 +00:00
|
|
|
*
|
|
|
|
* @param int $zone_id Zone ID.
|
2015-12-11 14:11:12 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
2016-04-22 14:45:34 +00:00
|
|
|
public static function delete_zone( $zone_id ) {
|
2016-02-10 13:21:16 +00:00
|
|
|
$zone = new WC_Shipping_Zone( $zone_id );
|
2016-02-05 15:20:17 +00:00
|
|
|
$zone->delete();
|
2015-12-15 12:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a matching zone for a given package.
|
2017-12-06 21:11:40 +00:00
|
|
|
*
|
2015-12-15 12:39:52 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
* @uses wc_make_numeric_postcode()
|
2017-12-06 21:11:40 +00:00
|
|
|
* @param array $package Shipping package.
|
2015-12-15 12:39:52 +00:00
|
|
|
* @return WC_Shipping_Zone
|
|
|
|
*/
|
|
|
|
public static function get_zone_matching_package( $package ) {
|
|
|
|
$country = strtoupper( wc_clean( $package['destination']['country'] ) );
|
|
|
|
$state = strtoupper( wc_clean( $package['destination']['state'] ) );
|
2016-06-06 15:17:15 +00:00
|
|
|
$postcode = wc_normalize_postcode( wc_clean( $package['destination']['postcode'] ) );
|
2015-12-15 12:39:52 +00:00
|
|
|
$cache_key = WC_Cache_Helper::get_cache_prefix( 'shipping_zones' ) . 'wc_shipping_zone_' . md5( sprintf( '%s+%s+%s', $country, $state, $postcode ) );
|
|
|
|
$matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' );
|
|
|
|
|
|
|
|
if ( false === $matching_zone_id ) {
|
2016-11-15 22:55:14 +00:00
|
|
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
|
|
|
$matching_zone_id = $data_store->get_zone_id_from_package( $package );
|
2015-12-15 12:39:52 +00:00
|
|
|
wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WC_Shipping_Zone( $matching_zone_id ? $matching_zone_id : 0 );
|
|
|
|
}
|
2015-12-10 11:55:03 +00:00
|
|
|
}
|