Fixed wc_postcode_location_matcher logic

Due to the return values overwriting each other,
wc_postcode_location_matcher did not work with zones using multiple
postcodes. This changes the return to return an array of values instead
of a single value to fix that logic.

Also adjusts taxes which use wc_postcode_location_matcher so this
continues to function.

Fixes #11095

@claudiosmweb
This commit is contained in:
Mike Jolley 2016-06-11 00:26:07 +01:00
parent 3d1b29bd38
commit fe9b53e49a
2 changed files with 19 additions and 10 deletions

View File

@ -291,9 +291,15 @@ class WC_Tax {
if ( $postcode_ranges ) {
$matches = wc_postcode_location_matcher( $postcode, $postcode_ranges, 'tax_rate_id', 'location_code' );
$postcode_search = array_unique( array_merge( $postcode_search, array_values( $matches ) ) );
if ( ! empty( $matches ) ) {
foreach ( $matches as $matched_postcodes ) {
$postcode_search = array_merge( $postcode_search, $matched_postcodes );
}
}
}
$postcode_search = array_unique( $postcode_search );
/**
* Location matching criteria - ORed
* Needs to match:

View File

@ -1249,19 +1249,18 @@ function wc_get_wildcard_postcodes( $postcode ) {
* @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 string $object_compare_key DB column name for the ID.
* @param string $object_id_key DB column name for the ID.
* @param string $object_compare_key DB column name for the value.
* @param string $object_id_key
* @return array Array of matching object ID and values.
* @return array Array of matching object ID and matching values.
*/
function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key ) {
$postcode = wc_normalize_postcode( $postcode );
$wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode ) );
$postcodes = array_map( 'wc_normalize_postcode', wp_list_pluck( $objects, $object_compare_key, $object_id_key ) );
$matches = array();
foreach ( $postcodes as $object_id => $compare_against ) {
$compare = $postcode;
foreach ( $objects as $object ) {
$object_id = $object->$object_id_key;
$compare_against = $object->$object_compare_key;
// Handle postcodes containing ranges.
if ( strstr( $compare_against, '...' ) ) {
@ -1275,18 +1274,22 @@ function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $obj
// If the postcode is non-numeric, make it numeric.
if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) {
$compare = wc_make_numeric_postcode( $compare );
$compare = wc_make_numeric_postcode( $postcode );
$min = str_pad( wc_make_numeric_postcode( $min ), strlen( $compare ), '0' );
$max = str_pad( wc_make_numeric_postcode( $max ), strlen( $compare ), '0' );
} else {
$compare = $postcode;
}
if ( $compare >= $min && $compare <= $max ) {
$matches[ $object_id ] = $compare_against;
$matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array();
$matches[ $object_id ][] = $compare_against;
}
// Wildcard and standard comparison.
} elseif ( in_array( $compare_against, $wildcard_postcodes ) ) {
$matches[ $object_id ] = $compare_against;
$matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array();
$matches[ $object_id ][] = $compare_against;
}
}