wc_postcode_location_matcher

Used by shipping zones and taxes to compare a given $postcode to stored postcodes to find matches for numerical ranges, and wildcards.

Description

(array) wc_postcode_location_matcher( (string) $postcode, (array) $objects, (string) $object_id_key, (string) $object_compare_key, (string) $country = '' ); 

Returns (array)

Array of matching object ID and matching values.

Parameters (5)

0. $postcode (string)
Postcode you want to match against stored postcodes
1. $objects (array)
Array of postcode objects from Database
2. $object_id_key (string)
DB column name for the ID.
3. $object_compare_key (string)
DB column name for the value.
4. $country — Optional. (string) => ''
Country from which this postcode belongs. Allows for formatting.

Usage

  1. if ( !function_exists( 'wc_postcode_location_matcher' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-core-functions.php'; 
  3.  
  4. // Postcode you want to match against stored postcodes 
  5. $postcode = ''; 
  6.  
  7. // Array of postcode objects from Database 
  8. $objects = array(); 
  9.  
  10. // DB column name for the ID. 
  11. $object_id_key = ''; 
  12.  
  13. // DB column name for the value. 
  14. $object_compare_key = ''; 
  15.  
  16. // Country from which this postcode belongs. Allows for formatting. 
  17. $country = ''; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = wc_postcode_location_matcher($postcode, $objects, $object_id_key, $object_compare_key, $country); 
  21.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-core-functions.php  
  1. function wc_postcode_location_matcher( $postcode, $objects, $object_id_key, $object_compare_key, $country = '' ) { 
  2. $postcode = wc_normalize_postcode( $postcode ); 
  3. $wildcard_postcodes = array_map( 'wc_clean', wc_get_wildcard_postcodes( $postcode, $country ) ); 
  4. $matches = array(); 
  5.  
  6. foreach ( $objects as $object ) { 
  7. $object_id = $object->$object_id_key; 
  8. $compare_against = $object->$object_compare_key; 
  9.  
  10. // Handle postcodes containing ranges. 
  11. if ( strstr( $compare_against, '...' ) ) { 
  12. $range = array_map( 'trim', explode( '...', $compare_against ) ); 
  13.  
  14. if ( 2 !== sizeof( $range ) ) { 
  15. continue; 
  16.  
  17. list( $min, $max ) = $range; 
  18.  
  19. // If the postcode is non-numeric, make it numeric. 
  20. if ( ! is_numeric( $min ) || ! is_numeric( $max ) ) { 
  21. $compare = wc_make_numeric_postcode( $postcode ); 
  22. $min = str_pad( wc_make_numeric_postcode( $min ), strlen( $compare ), '0' ); 
  23. $max = str_pad( wc_make_numeric_postcode( $max ), strlen( $compare ), '0' ); 
  24. } else { 
  25. $compare = $postcode; 
  26.  
  27. if ( $compare >= $min && $compare <= $max ) { 
  28. $matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array(); 
  29. $matches[ $object_id ][] = $compare_against; 
  30.  
  31. // Wildcard and standard comparison. 
  32. } elseif ( in_array( $compare_against, $wildcard_postcodes ) ) { 
  33. $matches[ $object_id ] = isset( $matches[ $object_id ] ) ? $matches[ $object_id ]: array(); 
  34. $matches[ $object_id ][] = $compare_against; 
  35.  
  36. return $matches;