Created
October 7, 2019 19:00
-
-
Save Amourspirit/cf0f3bac154f816ff2d597085bbbe52f to your computer and use it in GitHub Desktop.
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 | |
/** | |
* WooCommerce Match Canada Postal Codes | |
* | |
* Checks WooCommerce Postal Code and removes gateways if not matching Canada Postal Format | |
*/ | |
add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ){ | |
global $woocommerce; | |
if ( is_admin() ) { | |
return $available_gateways; | |
} | |
$user_postal = !empty($woocommerce->customer->get_shipping_postcode()) ? $woocommerce->customer->get_shipping_postcode() : $woocommerce->customer->get_billing_postcode(); | |
$regex = '/^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$/'; | |
$remove = false; | |
if (!preg_match($regex, $user_postal)) { | |
$remove = true; | |
} | |
if($remove) { | |
return array(); | |
} | |
return $available_gateways; | |
},20, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WordPress - WooCommerce - Matches Canada style Postal Codes and removes all payment gateways if invalid match.
Add to your template functions file or in a snippet plugin.