Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Amourspirit/cf0f3bac154f816ff2d597085bbbe52f to your computer and use it in GitHub Desktop.
Save Amourspirit/cf0f3bac154f816ff2d597085bbbe52f to your computer and use it in GitHub Desktop.
<?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);
@Amourspirit
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment