Finish moving queries to the shipping zone data store, update usage of deprecated get_zone_id, legacy updates.
This commit is contained in:
parent
61d1b29789
commit
735c1b7a14
|
@ -235,7 +235,7 @@ class WC_Settings_Shipping extends WC_Settings_Page {
|
||||||
wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array(
|
wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array(
|
||||||
'methods' => $zone->get_shipping_methods(),
|
'methods' => $zone->get_shipping_methods(),
|
||||||
'zone_name' => $zone->get_zone_name(),
|
'zone_name' => $zone->get_zone_name(),
|
||||||
'zone_id' => $zone->get_zone_id(),
|
'zone_id' => $zone->get_id(),
|
||||||
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
|
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
|
||||||
'strings' => array(
|
'strings' => array(
|
||||||
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
|
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
|
||||||
|
|
|
@ -13,7 +13,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
|
||||||
<table class="form-table wc-shipping-zone-settings">
|
<table class="form-table wc-shipping-zone-settings">
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php if ( 0 !== $zone->get_zone_id() ) : ?>
|
<?php if ( 0 !== $zone->get_id() ) : ?>
|
||||||
<tr valign="top" class="">
|
<tr valign="top" class="">
|
||||||
<th scope="row" class="titledesc">
|
<th scope="row" class="titledesc">
|
||||||
<label for="zone_name"><?php esc_html_e( 'Zone name', 'woocommerce' ); ?></label>
|
<label for="zone_name"><?php esc_html_e( 'Zone name', 'woocommerce' ); ?></label>
|
||||||
|
|
|
@ -5,7 +5,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
?>
|
?>
|
||||||
<h2>
|
<h2>
|
||||||
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=shipping' ); ?>"><?php _e( 'Shipping zones', 'woocommerce' ); ?></a> >
|
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=shipping' ); ?>"><?php _e( 'Shipping zones', 'woocommerce' ); ?></a> >
|
||||||
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=shipping&zone_id=' . absint( $zone->get_zone_id() ) ); ?>"><?php echo esc_html( $zone->get_zone_name() ); ?></a> >
|
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=shipping&zone_id=' . absint( $zone->get_id() ) ); ?>"><?php echo esc_html( $zone->get_zone_name() ); ?></a> >
|
||||||
<?php echo esc_html( $shipping_method->get_method_title() ); ?>
|
<?php echo esc_html( $shipping_method->get_method_title() ); ?>
|
||||||
</h2>
|
</h2>
|
||||||
|
|
||||||
|
|
|
@ -2469,7 +2469,7 @@ class WC_AJAX {
|
||||||
|
|
||||||
wp_send_json_success( array(
|
wp_send_json_success( array(
|
||||||
'instance_id' => $instance_id,
|
'instance_id' => $instance_id,
|
||||||
'zone_id' => $zone->get_zone_id(),
|
'zone_id' => $zone->get_id(),
|
||||||
'zone_name' => $zone->get_zone_name(),
|
'zone_name' => $zone->get_zone_name(),
|
||||||
'methods' => $zone->get_shipping_methods(),
|
'methods' => $zone->get_shipping_methods(),
|
||||||
) );
|
) );
|
||||||
|
@ -2567,7 +2567,7 @@ class WC_AJAX {
|
||||||
$zone->save();
|
$zone->save();
|
||||||
|
|
||||||
wp_send_json_success( array(
|
wp_send_json_success( array(
|
||||||
'zone_id' => $zone->get_zone_id(),
|
'zone_id' => $zone->get_id(),
|
||||||
'zone_name' => $zone->get_zone_name(),
|
'zone_name' => $zone->get_zone_name(),
|
||||||
'methods' => $zone->get_shipping_methods(),
|
'methods' => $zone->get_shipping_methods(),
|
||||||
) );
|
) );
|
||||||
|
@ -2599,7 +2599,7 @@ class WC_AJAX {
|
||||||
$shipping_method->process_admin_options();
|
$shipping_method->process_admin_options();
|
||||||
|
|
||||||
wp_send_json_success( array(
|
wp_send_json_success( array(
|
||||||
'zone_id' => $zone->get_zone_id(),
|
'zone_id' => $zone->get_id(),
|
||||||
'zone_name' => $zone->get_zone_name(),
|
'zone_name' => $zone->get_zone_name(),
|
||||||
'methods' => $zone->get_shipping_methods(),
|
'methods' => $zone->get_shipping_methods(),
|
||||||
'errors' => $shipping_method->get_errors(),
|
'errors' => $shipping_method->get_errors(),
|
||||||
|
|
|
@ -17,6 +17,10 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
*/
|
*/
|
||||||
class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zone ID
|
||||||
|
* @var int|null
|
||||||
|
*/
|
||||||
protected $id = null;
|
protected $id = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,8 +34,9 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for zones
|
* Constructor for zones.
|
||||||
* @param int|object $zone Zone ID to load from the DB (optional) or already queried data.
|
*
|
||||||
|
* @param int|object $zone Zone ID to load from the DB or zone object.
|
||||||
*/
|
*/
|
||||||
public function __construct( $zone = null ) {
|
public function __construct( $zone = null ) {
|
||||||
if ( is_numeric( $zone ) && ! empty( $zone ) ) {
|
if ( is_numeric( $zone ) && ! empty( $zone ) ) {
|
||||||
|
@ -51,34 +56,14 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Returns all data for this object.
|
|--------------------------------------------------------------------------
|
||||||
* @return array
|
| Getters
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
public function get_data() {
|
|
||||||
return array_merge( array( 'id' => $this->get_id(), 'zone_id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save zone data to the database.
|
* Get zone name.
|
||||||
*/
|
|
||||||
public function save() {
|
|
||||||
$name = $this->get_zone_name();
|
|
||||||
if ( empty( $name ) ) {
|
|
||||||
$this->set_zone_name( $this->generate_zone_name() );
|
|
||||||
}
|
|
||||||
if ( $this->data_store ) {
|
|
||||||
if ( null === $this->get_id() ) {
|
|
||||||
$this->data_store->create( $this );
|
|
||||||
} else {
|
|
||||||
$this->data_store->update( $this );
|
|
||||||
}
|
|
||||||
return $this->get_id();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get zone name
|
|
||||||
*
|
*
|
||||||
* @param string $context
|
* @param string $context
|
||||||
* @return string
|
* @return string
|
||||||
|
@ -88,7 +73,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get zone order
|
* Get zone order.
|
||||||
*
|
*
|
||||||
* @param string $context
|
* @param string $context
|
||||||
* @return int
|
* @return int
|
||||||
|
@ -98,7 +83,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get zone locations
|
* Get zone locations.
|
||||||
*
|
*
|
||||||
* @param string $context
|
* @param string $context
|
||||||
* @return array of zone objects
|
* @return array of zone objects
|
||||||
|
@ -107,20 +92,6 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
return $this->get_prop( 'zone_locations', $context );
|
return $this->get_prop( 'zone_locations', $context );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a zone name based on location.
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
protected function generate_zone_name() {
|
|
||||||
$zone_name = $this->get_formatted_location();
|
|
||||||
|
|
||||||
if ( empty( $zone_name ) ) {
|
|
||||||
$zone_name = __( 'Zone', 'woocommerce' );
|
|
||||||
}
|
|
||||||
|
|
||||||
return $zone_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a text string representing what this zone is for.
|
* Return a text string representing what this zone is for.
|
||||||
*
|
*
|
||||||
|
@ -172,19 +143,17 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get shipping methods linked to this zone
|
* Get shipping methods linked to this zone.
|
||||||
|
*
|
||||||
* @param bool Only return enabled methods.
|
* @param bool Only return enabled methods.
|
||||||
* @return array of objects
|
* @return array of objects
|
||||||
*/
|
*/
|
||||||
public function get_shipping_methods( $enabled_only = false ) {
|
public function get_shipping_methods( $enabled_only = false ) {
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
if ( null === $this->get_id() ) {
|
if ( null === $this->get_id() ) {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
$raw_methods_sql = $enabled_only ? "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d AND is_enabled = 1;" : "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d;";
|
$raw_methods = $this->data_store->get_methods( $this->get_id(), $enabled_only );
|
||||||
$raw_methods = $wpdb->get_results( $wpdb->prepare( $raw_methods_sql, $this->get_id() ) );
|
|
||||||
$wc_shipping = WC_Shipping::instance();
|
$wc_shipping = WC_Shipping::instance();
|
||||||
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
||||||
$methods = array();
|
$methods = array();
|
||||||
|
@ -223,44 +192,15 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
return apply_filters( 'woocommerce_shipping_zone_shipping_methods', $methods, $raw_methods, $allowed_classes, $this );
|
return apply_filters( 'woocommerce_shipping_zone_shipping_methods', $methods, $raw_methods, $allowed_classes, $this );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Location type detection
|
|--------------------------------------------------------------------------
|
||||||
* @param object $location
|
| Setters
|
||||||
* @return boolean
|
|--------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
private function location_is_continent( $location ) {
|
|
||||||
return 'continent' === $location->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Location type detection
|
* Set zone name.
|
||||||
* @param object $location
|
*
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
private function location_is_country( $location ) {
|
|
||||||
return 'country' === $location->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Location type detection
|
|
||||||
* @param object $location
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
private function location_is_state( $location ) {
|
|
||||||
return 'state' === $location->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Location type detection
|
|
||||||
* @param object $location
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
private function location_is_postcode( $location ) {
|
|
||||||
return 'postcode' === $location->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set zone name
|
|
||||||
* @param string $set
|
* @param string $set
|
||||||
*/
|
*/
|
||||||
public function set_zone_name( $set ) {
|
public function set_zone_name( $set ) {
|
||||||
|
@ -268,7 +208,8 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set zone order
|
* Set zone order.
|
||||||
|
*
|
||||||
* @param int $set
|
* @param int $set
|
||||||
*/
|
*/
|
||||||
public function set_zone_order( $set ) {
|
public function set_zone_order( $set ) {
|
||||||
|
@ -276,7 +217,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set zone locations
|
* Set zone locations.
|
||||||
*
|
*
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
* @param array
|
* @param array
|
||||||
|
@ -285,8 +226,90 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
$this->set_prop( 'zone_locations', $locations );
|
$this->set_prop( 'zone_locations', $locations );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| Other Methods
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save zone data to the database.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function save() {
|
||||||
|
$name = $this->get_zone_name();
|
||||||
|
if ( empty( $name ) ) {
|
||||||
|
$this->set_zone_name( $this->generate_zone_name() );
|
||||||
|
}
|
||||||
|
if ( $this->data_store ) {
|
||||||
|
if ( null === $this->get_id() ) {
|
||||||
|
$this->data_store->create( $this );
|
||||||
|
} else {
|
||||||
|
$this->data_store->update( $this );
|
||||||
|
}
|
||||||
|
return $this->get_id();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a zone name based on location.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function generate_zone_name() {
|
||||||
|
$zone_name = $this->get_formatted_location();
|
||||||
|
|
||||||
|
if ( empty( $zone_name ) ) {
|
||||||
|
$zone_name = __( 'Zone', 'woocommerce' );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $zone_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Location type detection.
|
||||||
|
*
|
||||||
|
* @param object $location
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function location_is_continent( $location ) {
|
||||||
|
return 'continent' === $location->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Location type detection.
|
||||||
|
*
|
||||||
|
* @param object $location
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function location_is_country( $location ) {
|
||||||
|
return 'country' === $location->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Location type detection.
|
||||||
|
*
|
||||||
|
* @param object $location
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function location_is_state( $location ) {
|
||||||
|
return 'state' === $location->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Location type detection.
|
||||||
|
*
|
||||||
|
* @param object $location
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
private function location_is_postcode( $location ) {
|
||||||
|
return 'postcode' === $location->type;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is passed location type valid?
|
* Is passed location type valid?
|
||||||
|
*
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
@ -296,6 +319,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add location (state or postcode) to a zone.
|
* Add location (state or postcode) to a zone.
|
||||||
|
*
|
||||||
* @param string $code
|
* @param string $code
|
||||||
* @param string $type state or postcode
|
* @param string $type state or postcode
|
||||||
*/
|
*/
|
||||||
|
@ -317,6 +341,7 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear all locations for this zone.
|
* Clear all locations for this zone.
|
||||||
|
*
|
||||||
* @param array|string $types of location to clear
|
* @param array|string $types of location to clear
|
||||||
*/
|
*/
|
||||||
public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) {
|
public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) {
|
||||||
|
@ -333,7 +358,8 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set locations
|
* Set locations.
|
||||||
|
*
|
||||||
* @param array $locations Array of locations
|
* @param array $locations Array of locations
|
||||||
*/
|
*/
|
||||||
public function set_locations( $locations = array() ) {
|
public function set_locations( $locations = array() ) {
|
||||||
|
@ -345,12 +371,11 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a shipping method to this zone.
|
* Add a shipping method to this zone.
|
||||||
|
*
|
||||||
* @param string $type shipping method type
|
* @param string $type shipping method type
|
||||||
* @return int new instance_id, 0 on failure
|
* @return int new instance_id, 0 on failure
|
||||||
*/
|
*/
|
||||||
public function add_shipping_method( $type ) {
|
public function add_shipping_method( $type ) {
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
if ( null === $this->get_id() ) {
|
if ( null === $this->get_id() ) {
|
||||||
$this->save();
|
$this->save();
|
||||||
}
|
}
|
||||||
|
@ -358,23 +383,10 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
$instance_id = 0;
|
$instance_id = 0;
|
||||||
$wc_shipping = WC_Shipping::instance();
|
$wc_shipping = WC_Shipping::instance();
|
||||||
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
||||||
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d", $this->get_id() ) );
|
$count = $this->data_store->get_method_count( $this->get_id() );
|
||||||
|
|
||||||
if ( in_array( $type, array_keys( $allowed_classes ) ) ) {
|
if ( in_array( $type, array_keys( $allowed_classes ) ) ) {
|
||||||
$wpdb->insert(
|
$instance_id = $this->data_store->add_method( $this->get_id(), $type, $count + 1 );
|
||||||
$wpdb->prefix . 'woocommerce_shipping_zone_methods',
|
|
||||||
array(
|
|
||||||
'method_id' => $type,
|
|
||||||
'zone_id' => $this->get_id(),
|
|
||||||
'method_order' => ( $count + 1 ),
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'%s',
|
|
||||||
'%d',
|
|
||||||
'%d',
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$instance_id = $wpdb->insert_id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $instance_id ) {
|
if ( $instance_id ) {
|
||||||
|
@ -388,17 +400,16 @@ class WC_Shipping_Zone extends WC_Legacy_Shipping_Zone {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a shipping method from a zone.
|
* Delete a shipping method from a zone.
|
||||||
|
*
|
||||||
* @param int $instance_id
|
* @param int $instance_id
|
||||||
* @return True on success, false on failure
|
* @return True on success, false on failure
|
||||||
*/
|
*/
|
||||||
public function delete_shipping_method( $instance_id ) {
|
public function delete_shipping_method( $instance_id ) {
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
if ( null === $this->get_id() ) {
|
if ( null === $this->get_id() ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'instance_id' => $instance_id ) );
|
$this->data_store->delete_method( $instance_id );
|
||||||
do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $this->get_id() );
|
do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $this->get_id() );
|
||||||
|
|
||||||
WC_Cache_Helper::get_transient_version( 'shipping', true );
|
WC_Cache_Helper::get_transient_version( 'shipping', true );
|
||||||
|
|
|
@ -22,16 +22,15 @@ class WC_Shipping_Zones {
|
||||||
* @return array of arrays
|
* @return array of arrays
|
||||||
*/
|
*/
|
||||||
public static function get_zones() {
|
public static function get_zones() {
|
||||||
global $wpdb;
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
||||||
|
$raw_zones = $data_store->get_zones();
|
||||||
$raw_zones = $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;" );
|
$zones = array();
|
||||||
$zones = array();
|
|
||||||
|
|
||||||
foreach ( $raw_zones as $raw_zone ) {
|
foreach ( $raw_zones as $raw_zone ) {
|
||||||
$zone = new WC_Shipping_Zone( $raw_zone );
|
$zone = new WC_Shipping_Zone( $raw_zone );
|
||||||
$zones[ $zone->get_zone_id() ] = $zone->get_data();
|
$zones[ $zone->get_id() ] = $zone->get_data();
|
||||||
$zones[ $zone->get_zone_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
|
$zones[ $zone->get_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
|
||||||
$zones[ $zone->get_zone_id() ]['shipping_methods'] = $zone->get_shipping_methods();
|
$zones[ $zone->get_id() ]['shipping_methods'] = $zone->get_shipping_methods();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $zones;
|
return $zones;
|
||||||
|
@ -55,15 +54,15 @@ class WC_Shipping_Zones {
|
||||||
* @return WC_Shipping_Zone|bool
|
* @return WC_Shipping_Zone|bool
|
||||||
*/
|
*/
|
||||||
public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
|
public static function get_zone_by( $by = 'zone_id', $id = 0 ) {
|
||||||
global $wpdb;
|
|
||||||
switch ( $by ) {
|
switch ( $by ) {
|
||||||
case 'zone_id' :
|
case 'zone_id' :
|
||||||
return new WC_Shipping_Zone( $id );
|
return new WC_Shipping_Zone( $id );
|
||||||
break;
|
break;
|
||||||
case 'instance_id' :
|
case 'instance_id' :
|
||||||
$zone_id = $wpdb->get_var( $wpdb->prepare( "SELECT zone_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods as methods WHERE methods.instance_id = %d LIMIT 1;", $id ) );
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
||||||
|
$zone_id = $data_store->get_zone_id_by_instance_id( $id );
|
||||||
if ( false !== $zone_id ) {
|
if ( false !== $zone_id ) {
|
||||||
return self::get_zone_by( 'zone_id', absint( $zone_id ) );
|
return new WC_Shipping_Zone( $zone_id );
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -77,8 +76,8 @@ class WC_Shipping_Zones {
|
||||||
* @return WC_Shipping_Meethod|bool
|
* @return WC_Shipping_Meethod|bool
|
||||||
*/
|
*/
|
||||||
public static function get_shipping_method( $instance_id ) {
|
public static function get_shipping_method( $instance_id ) {
|
||||||
global $wpdb;
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
||||||
$raw_shipping_method = $wpdb->get_row( $wpdb->prepare( "SELECT instance_id, method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d LIMIT 1;", $instance_id ) );
|
$raw_shipping_method = $data_store->get_method( $instance_id );
|
||||||
$wc_shipping = WC_Shipping::instance();
|
$wc_shipping = WC_Shipping::instance();
|
||||||
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
$allowed_classes = $wc_shipping->get_shipping_method_class_names();
|
||||||
|
|
||||||
|
@ -110,8 +109,6 @@ class WC_Shipping_Zones {
|
||||||
* @return WC_Shipping_Zone
|
* @return WC_Shipping_Zone
|
||||||
*/
|
*/
|
||||||
public static function get_zone_matching_package( $package ) {
|
public static function get_zone_matching_package( $package ) {
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
$country = strtoupper( wc_clean( $package['destination']['country'] ) );
|
$country = strtoupper( wc_clean( $package['destination']['country'] ) );
|
||||||
$state = strtoupper( wc_clean( $package['destination']['state'] ) );
|
$state = strtoupper( wc_clean( $package['destination']['state'] ) );
|
||||||
$continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
|
$continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
|
||||||
|
@ -120,35 +117,8 @@ class WC_Shipping_Zones {
|
||||||
$matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' );
|
$matching_zone_id = wp_cache_get( $cache_key, 'shipping_zones' );
|
||||||
|
|
||||||
if ( false === $matching_zone_id ) {
|
if ( false === $matching_zone_id ) {
|
||||||
|
$data_store = WC_Data_Store::load( 'shipping-zone' );
|
||||||
// Work out criteria for our zone search
|
$matching_zone_id = $data_store->get_zone_id_from_package( $package );
|
||||||
$criteria = array();
|
|
||||||
$criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country );
|
|
||||||
$criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state );
|
|
||||||
$criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s )", $continent );
|
|
||||||
$criteria[] = "OR ( location_type IS NULL ) )";
|
|
||||||
|
|
||||||
// Postcode range and wildcard matching
|
|
||||||
$postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );
|
|
||||||
|
|
||||||
if ( $postcode_locations ) {
|
|
||||||
$zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) );
|
|
||||||
$matches = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code', $country );
|
|
||||||
$do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, array_keys( $matches ) ) );
|
|
||||||
|
|
||||||
if ( ! empty( $do_not_match ) ) {
|
|
||||||
$criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get matching zones
|
|
||||||
$matching_zone_id = $wpdb->get_var( "
|
|
||||||
SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
|
|
||||||
LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id AND location_type != 'postcode'
|
|
||||||
WHERE " . implode( ' ', $criteria ) . "
|
|
||||||
ORDER BY zone_order ASC LIMIT 1
|
|
||||||
" );
|
|
||||||
|
|
||||||
wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' );
|
wp_cache_set( $cache_key, $matching_zone_id, 'shipping_zones' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -97,6 +97,150 @@ class WC_Shipping_Zone_Data_Store_Table implements WC_Shipping_Zone_Data_Store_I
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of shipping methods for a specific sone.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param int $zone_id Zone ID
|
||||||
|
* @param bool $enabled_only True to request enabled methods only.
|
||||||
|
* @return array Array of objects containing method_id, method_order, instance_id, is_enabled
|
||||||
|
*/
|
||||||
|
public function get_methods( $zone_id, $enabled_only ) {
|
||||||
|
global $wpdb;
|
||||||
|
$raw_methods_sql = $enabled_only ? "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d AND is_enabled = 1;" : "SELECT method_id, method_order, instance_id, is_enabled FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d;";
|
||||||
|
return $wpdb->get_results( $wpdb->prepare( $raw_methods_sql, $zone_id ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of methods for a zone.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param int Zone ID
|
||||||
|
* @return int Method Count
|
||||||
|
*/
|
||||||
|
public function get_method_count( $zone_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE zone_id = %d", $zone_id ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a shipping method to a zone.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param int $zone_id Zone ID
|
||||||
|
* @param string $type Method Type/ID
|
||||||
|
* @param int $order Method Order
|
||||||
|
* @return int Instance ID
|
||||||
|
*/
|
||||||
|
public function add_method( $zone_id, $type, $order ) {
|
||||||
|
global $wpdb;
|
||||||
|
$wpdb->insert(
|
||||||
|
$wpdb->prefix . 'woocommerce_shipping_zone_methods',
|
||||||
|
array(
|
||||||
|
'method_id' => $type,
|
||||||
|
'zone_id' => $zone_id,
|
||||||
|
'method_order' => $order,
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'%s',
|
||||||
|
'%d',
|
||||||
|
'%d',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return $wpdb->insert_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a method instance.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param int $instance_id
|
||||||
|
*/
|
||||||
|
public function delete_method( $instance_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 'instance_id' => $instance_id ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a shipping zone method instance.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param int
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function get_method( $instance_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d LIMIT 1;", $instance_id ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a matching zone ID for a given package.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param object $package
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function get_zone_id_from_package( $package ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$country = strtoupper( wc_clean( $package['destination']['country'] ) );
|
||||||
|
$state = strtoupper( wc_clean( $package['destination']['state'] ) );
|
||||||
|
$continent = strtoupper( wc_clean( WC()->countries->get_continent_code_for_country( $country ) ) );
|
||||||
|
$postcode = wc_normalize_postcode( wc_clean( $package['destination']['postcode'] ) );
|
||||||
|
|
||||||
|
// Work out criteria for our zone search
|
||||||
|
$criteria = array();
|
||||||
|
$criteria[] = $wpdb->prepare( "( ( location_type = 'country' AND location_code = %s )", $country );
|
||||||
|
$criteria[] = $wpdb->prepare( "OR ( location_type = 'state' AND location_code = %s )", $country . ':' . $state );
|
||||||
|
$criteria[] = $wpdb->prepare( "OR ( location_type = 'continent' AND location_code = %s )", $continent );
|
||||||
|
$criteria[] = "OR ( location_type IS NULL ) )";
|
||||||
|
|
||||||
|
// Postcode range and wildcard matching
|
||||||
|
$postcode_locations = $wpdb->get_results( "SELECT zone_id, location_code FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE location_type = 'postcode';" );
|
||||||
|
|
||||||
|
if ( $postcode_locations ) {
|
||||||
|
$zone_ids_with_postcode_rules = array_map( 'absint', wp_list_pluck( $postcode_locations, 'zone_id' ) );
|
||||||
|
$matches = wc_postcode_location_matcher( $postcode, $postcode_locations, 'zone_id', 'location_code', $country );
|
||||||
|
$do_not_match = array_unique( array_diff( $zone_ids_with_postcode_rules, array_keys( $matches ) ) );
|
||||||
|
|
||||||
|
if ( ! empty( $do_not_match ) ) {
|
||||||
|
$criteria[] = "AND zones.zone_id NOT IN (" . implode( ',', $do_not_match ) . ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get matching zones
|
||||||
|
return $wpdb->get_var( "
|
||||||
|
SELECT zones.zone_id FROM {$wpdb->prefix}woocommerce_shipping_zones as zones
|
||||||
|
LEFT OUTER JOIN {$wpdb->prefix}woocommerce_shipping_zone_locations as locations ON zones.zone_id = locations.zone_id AND location_type != 'postcode'
|
||||||
|
WHERE " . implode( ' ', $criteria ) . "
|
||||||
|
ORDER BY zone_order ASC LIMIT 1
|
||||||
|
" );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an ordered list of zones.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @return array An array of objects containing a zone_id, zone_name, and zone_order.
|
||||||
|
*/
|
||||||
|
public function get_zones() {
|
||||||
|
global $wpdb;
|
||||||
|
return $wpdb->get_results( "SELECT zone_id, zone_name, zone_order FROM {$wpdb->prefix}woocommerce_shipping_zones order by zone_order ASC;" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a zone ID from an instance ID.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param int
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function get_zone_id_by_instance_id( $id ) {
|
||||||
|
global $wpdb;
|
||||||
|
return $wpdb->get_var( $wpdb->prepare( "SELECT zone_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods as methods WHERE methods.instance_id = %d LIMIT 1;", $id ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read location data from the database.
|
* Read location data from the database.
|
||||||
*
|
*
|
||||||
|
@ -136,5 +280,4 @@ class WC_Shipping_Zone_Data_Store_Table implements WC_Shipping_Zone_Data_Store_I
|
||||||
) );
|
) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,5 +13,60 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
* @author WooCommerce
|
* @author WooCommerce
|
||||||
*/
|
*/
|
||||||
interface WC_Shipping_Zone_Data_Store_Interface {
|
interface WC_Shipping_Zone_Data_Store_Interface {
|
||||||
|
/**
|
||||||
|
* Get a list of shipping methods for a specific sone.
|
||||||
|
* @param int $zone_id Zone ID
|
||||||
|
* @param bool $enabled_only True to request enabled methods only.
|
||||||
|
* @return array Array of objects containing method_id, method_order, instance_id, is_enabled
|
||||||
|
*/
|
||||||
|
public function get_methods( $zone_id, $enabled_only );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of methods for a zone.
|
||||||
|
* @param int Zone ID
|
||||||
|
* @return int Method Count
|
||||||
|
*/
|
||||||
|
public function get_method_count( $zone_id );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a shipping method to a zone.
|
||||||
|
* @param int $zone_id Zone ID
|
||||||
|
* @param string $type Method Type/ID
|
||||||
|
* @param int $order Method Order
|
||||||
|
* @return int Instance ID
|
||||||
|
*/
|
||||||
|
public function add_method( $zone_id, $type, $order );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a method instance.
|
||||||
|
* @param int $instance_id
|
||||||
|
*/
|
||||||
|
public function delete_method( $instance_id );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a shipping zone method instance.
|
||||||
|
* @param int
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function get_method( $instance_id );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a matching zone ID for a given package.
|
||||||
|
* @param object $package
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function get_zone_id_from_package( $package );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an ordered list of zones.
|
||||||
|
* @return array An array of objects containing a zone_id, zone_name, and zone_order.
|
||||||
|
*/
|
||||||
|
public function get_zones();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a zone ID from an instance ID.
|
||||||
|
* @param int
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function get_zone_id_by_instance_id( $id );
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ class WC_Tests_Shipping_Zone extends WC_Unit_Test_Case {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test: WC_Shipping_Zones::get_zone_id
|
* Test: WC_Shipping_Zones::get_id
|
||||||
*/
|
*/
|
||||||
public function test_get_zone_id() {
|
public function test_get_zone_id() {
|
||||||
// Setup
|
// Setup
|
||||||
|
@ -35,7 +35,7 @@ class WC_Tests_Shipping_Zone extends WC_Unit_Test_Case {
|
||||||
$zone = WC_Shipping_Zones::get_zone( 1 );
|
$zone = WC_Shipping_Zones::get_zone( 1 );
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
$this->assertEquals( $zone->get_zone_id(), 1 );
|
$this->assertEquals( $zone->get_id(), 1 );
|
||||||
|
|
||||||
// Clean
|
// Clean
|
||||||
WC_Helper_Shipping_Zones::remove_mock_zones();
|
WC_Helper_Shipping_Zones::remove_mock_zones();
|
||||||
|
|
Loading…
Reference in New Issue