wc_postcode_location_matcher( string $postcode, array $objects, string $object_id_key, string $object_compare_key, string $country = '' )
Used by shipping zones and taxes to compare a given $postcode to stored postcodes to find matches for numerical ranges, and wildcards.
Description Description
Parameters Parameters
- $postcode
-
(Required) Postcode you want to match against stored postcodes.
- $objects
-
(Required) Array of postcode objects from Database.
- $object_id_key
-
(Required) DB column name for the ID.
- $object_compare_key
-
(Required) DB column name for the value.
- $country
-
(Optional) Country from which this postcode belongs. Allows for formatting.
Default value: ''
Return Return
(array) Array of matching object ID and matching values.
Source Source
File: includes/wc-core-functions.php
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, $country ) );
$matches = array();
foreach ( $objects as $object ) {
$object_id = $object->$object_id_key;
$compare_against = $object->$object_compare_key;
// Handle postcodes containing ranges.
if ( strstr( $compare_against, '...' ) ) {
$range = array_map( 'trim', explode( '...', $compare_against ) );
if ( 2 !== count( $range ) ) {
continue;
}
list( $min, $max ) = $range;
// If the postcode is non-numeric, make it numeric.
if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) {
$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 ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ] : array();
$matches[ $object_id ][] = $compare_against;
}
} elseif ( in_array( $compare_against, $wildcard_postcodes, true ) ) {
// Wildcard and standard comparison.
$matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ] : array();
$matches[ $object_id ][] = $compare_against;
}
}
return $matches;
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |