-
-
Save digitalchild/ff81b9702b303ea1c13138c740b0f72a to your computer and use it in GitHub Desktop.
Check postcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public static function check_postcode( $customer_postcode, $rate_postcode ){ | |
// clean both codes before doing anything to them | |
$customer_postcode = strtolower( str_replace( ' ', '', $customer_postcode ) ); | |
$rate_postcode = strtolower( str_replace( ' ', '', $rate_postcode ) ); | |
$postcode_length = strlen( $customer_postcode ); | |
// single post code to check? | |
if ( $customer_postcode == $rate_postcode ) return true; | |
// wildcard postcode ? | |
$wildcard_position = strpos( $rate_postcode, '*' ); | |
// The rate has a wildcard match only the beginning of both | |
if ( $wildcard_position > 0 ){ | |
$customer_postcode_start = substr( $customer_postcode, 0, $wildcard_position ); | |
$rate_postcode_start = substr( $rate_postcode, 0, $wildcard_position ); | |
return ( $customer_postcode_start === $rate_postcode_start ) ? true : false; | |
} | |
// check if the rate contains a range | |
$range_position = strpos( $rate_postcode, '-' ); | |
// postcode range set | |
if ( $range_position > 0 ){ | |
$range = array_map( 'trim', explode( apply_filters( 'wcv_shipping_postcode_range_separator', '-' ), $rate_postcode ) ); | |
$min_postcode = ( is_numeric( $range[ 0 ] ) ) ? $range[ 0 ] : make_numeric_postcode( $range[ 0 ] ); | |
$max_postcode = ( is_numeric( $range[ 1 ] ) ) ? $range[ 1 ] : make_numeric_postcode( $range[ 1 ] ); | |
// check if the ranges are the same size as the customer postcode | |
if ( $postcode_length != strlen( $min_postcode ) ) return false; | |
// Check to see if the postcode is in the range using filter_var | |
$in_range = filter_var( | |
$customer_postcode, | |
FILTER_VALIDATE_INT, | |
array( | |
'options' => array( | |
'min_range' => $min_postcode, | |
'max_range' => $max_postcode | |
) | |
) | |
); | |
// if there is a number in the range return what it finds | |
return is_int( $in_range ); | |
} | |
return false; | |
} // check_postcode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment