Skip to content

Commit

Permalink
Avoid normalization of zone postcodes so wildcard matching can be per…
Browse files Browse the repository at this point in the history
…formed on postcodes with spaces. E.g. SP1 *

Also adds matching zone notice when shipping debug is on.

fixes woocommerce#11154
  • Loading branch information
mikejolley committed Jun 16, 2016
1 parent fa07121 commit 7266e02
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion includes/class-wc-shipping-zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function is_valid_location_type( $type ) {
public function add_location( $code, $type ) {
if ( $this->is_valid_location_type( $type ) ) {
if ( 'postcode' === $type ) {
$code = wc_normalize_postcode( $code );
$code = trim( strtoupper( $code ) ); // No normalization - postcodes are matched against both normal and formatted versions to support wildcards.
}
$location = array(
'code' => wc_clean( $code ),
Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-shipping-zones.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static function get_zone_matching_package( $package ) {

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' );
$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 ) ) {
Expand Down
6 changes: 6 additions & 0 deletions includes/class-wc-shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ public function get_shipping_method_class_names() {
*/
public function load_shipping_methods( $package = array() ) {
if ( ! empty( $package ) ) {
$status_options = get_option( 'woocommerce_status_options', array() );
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$this->shipping_methods = $shipping_zone->get_shipping_methods( true );

// Debug output
if ( ! empty( $status_options['shipping_debug_mode'] ) ) {
wc_add_notice( 'Customer matched zone "' . $shipping_zone->get_zone_name() . '"' );
}
} else {
$this->shipping_methods = array();
}
Expand Down
7 changes: 4 additions & 3 deletions includes/class-wc-tax.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ private static function get_matched_tax_rates( $country, $state, $postcode, $cit
$criteria[] = $wpdb->prepare( "tax_rate_class = %s", sanitize_title( $tax_class ) );

// Pre-query postcode ranges for PHP based matching.
$postcode_search = wc_get_wildcard_postcodes( $postcode );
$postcode_search = wc_get_wildcard_postcodes( $postcode, $country );
$postcode_ranges = $wpdb->get_results( "SELECT tax_rate_id, location_code FROM {$wpdb->prefix}woocommerce_tax_rate_locations WHERE location_type = 'postcode' AND location_code LIKE '%...%';" );

if ( $postcode_ranges ) {
$matches = wc_postcode_location_matcher( $postcode, $postcode_ranges, 'tax_rate_id', 'location_code' );
$matches = wc_postcode_location_matcher( $postcode, $postcode_ranges, 'tax_rate_id', 'location_code', $country );
if ( ! empty( $matches ) ) {
foreach ( $matches as $matched_postcodes ) {
$postcode_search = array_merge( $postcode_search, $matched_postcodes );
Expand Down Expand Up @@ -818,7 +818,8 @@ public static function _update_tax_rate_postcodes( $tax_rate_id, $postcodes ) {
if ( ! is_array( $postcodes ) ) {
$postcodes = explode( ';', $postcodes );
}
self::_update_tax_rate_locations( $tax_rate_id, array_diff( array_map( 'wc_normalize_postcode', array_filter( $postcodes ) ), array( '*' ) ), 'postcode' );
// No normalization - postcodes are matched against both normal and formatted versions to support wildcards.
self::_update_tax_rate_locations( $tax_rate_id, array_diff( array_map( 'trim', array_map( 'strtoupper', array_filter( $postcodes ) ) ), array( '*' ) ), 'postcode' );
}

/**
Expand Down
21 changes: 12 additions & 9 deletions includes/wc-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,17 +1229,19 @@ function wc_help_tip( $tip, $allow_html = false ) {
* Return a list of potential postcodes for wildcard searching.
* @since 2.6.0
* @param string $postcode
* @param string $country to format postcode for matching.
* @return string[]
*/
function wc_get_wildcard_postcodes( $postcode ) {
$postcodes = array( '*', strtoupper( $postcode ), strtoupper( $postcode ) . '*' );
$postcode_length = strlen( $postcode );
$wildcard_postcode = strtoupper( $postcode );
function wc_get_wildcard_postcodes( $postcode, $country = '' ) {
$postcodes = array( $postcode );
$postcode = wc_format_postcode( $postcode, $country );
$postcodes[] = $postcode;
$postcode_length = strlen( $postcode );

for ( $i = 0; $i < $postcode_length; $i ++ ) {
$wildcard_postcode = substr( $wildcard_postcode, 0, -1 );
$postcodes[] = $wildcard_postcode . '*';
$postcodes[] = substr( $postcode, 0, ( $i + 1 ) * -1 ) . '*';
}

return $postcodes;
}

Expand All @@ -1248,14 +1250,15 @@ function wc_get_wildcard_postcodes( $postcode ) {
* postcodes to find matches for numerical ranges, and wildcards.
* @since 2.6.0
* @param string $postcode Postcode you want to match against stored postcodes
* @param array $objects Array of postcode objects from Database
* @param array $objects Array of postcode objects from Database
* @param string $object_id_key DB column name for the ID.
* @param string $object_compare_key DB column name for the value.
* @param string $country Country from which this postcode belongs. Allows for formatting.
* @return array Array of matching object ID and matching values.
*/
function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key ) {
function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) {
$postcode = wc_normalize_postcode( $postcode );
$wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode ) );
$wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode, $country ) );
$matches = array();

foreach ( $objects as $object ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/wc-formatting-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ function wc_format_postcode( $postcode, $country ) {
* @return string Sanitized postcode.
*/
function wc_normalize_postcode( $postcode ) {
return trim( preg_replace( '/[\s\-]/', '', strtoupper( $postcode ) ) );
return preg_replace( '/[\s\-]/', '', trim( strtoupper( $postcode ) ) );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,14 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Fix - Layered nav counts when term_id does not match term_taxonomy_id (before splitting).
* Fix - Fixed referer links from cart messages in WP 4.4.
* Dev - content-product.php and content-product_cat.php contained the wrong version.
* Dev - Show "matching zone" notice on the frontend when shipping debug mode is on.
* Dev - Restored missing WC_Settings_API::init_form_fields() method to prevent potential errors in 3rd party gateways.
* Dev - API - Fixed returned data from product images (changed `title` to `name`).
* Dev - API - Fixed products schema for `grouped_products`.
* Dev - API - Fixed products attribute options when contains `,`.
* Tweak - Hide 'payment methods' screen if no methods support it.
* Tweak - If shipping method count changes, reset to default.
* Tweak - Avoid normalization of zone postcodes so wildcard matching can be performed on postcodes with spaces. E.g. SP1 *

= 2.6.0 - 14/06/16 =
* Feature - Introduced Shipping Zone functionality, and re-usable instance based shipping methods.
Expand Down

0 comments on commit 7266e02

Please sign in to comment.