Are you looking to hide payment methods based on the billing pin code on the WooCommerce checkout page, then the snippet below is the solution.
add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ) { // if Cash on Delivery is already disabled, let's exit the function if( empty( $available_gateways['cod'] ) ) { return $available_gateways; } // get customer object $customer = WC()->customer; // check if customer object is not null if ( is_object( $customer ) ) { // get postal code $postal_code = $customer->get_billing_postcode(); // deactivate payment method if( in_array( $postal_code, array( '415709', '410208' ) ) ) { unset( $available_gateways['cod'] ); } } return $available_gateways; } );
Output
In the below output, the code snippet checks if “Cash on Delivery” is enabled. If it is, it looks at the customer’s billing postal code. If the postal code matches one of the specified values (in this case, ‘415709’ or ‘410208’), it disables the “Cash on Delivery” payment method.
![How to Hide Payment Methods Based on Billing Pin Code in WooCommerce? 1 How to Hide Payment Methods Based on Billing Pin Code in WooCommerce? - Tyche Softwares](https://media.tychesoftwares.com/wp-content/uploads/Hide-Payment-Method-on-Billing-Pin-Code-WooCommerce-1024x556.png)
Furthermore, you can also validate shipping zone code during the checkout in WooCommerce.
I mean, How can I do it using a ZIP Code range?
Hi Hugo, For your specific requirement, you can use the range() function to create an array of all ZIP codes within a specified range (e.g., from 560033 to 560045). Then, you can check if the customer’s postal code falls within that range, and if it does, the specified action (like disabling a payment method) will happen. Please try the modified code below: add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ) { // If Cash on Delivery is already disabled, exit the function if ( empty( $available_gateways['cod'] ) ) { return $available_gateways; } // Get the customer object $customer = WC()->customer; // Check if… Read more »
You are the best, thank you.
How can I do it using a CEP strip?
It is not working for me
Hi Ajay,
The code has been tested and works correctly with both the classic shortcode-based checkout page and in checkout blocks page in WooCommerce version 9.1.4. Please try switching to a default WordPress theme and deactivate other plugins except WooCommerce to check for conflicts. If the problem persists, let us know if you’ve made any additional customizations to your site so we can assist you better.