Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Hide Payment Methods Based on Billing Pin Code in WooCommerce?

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? - Tyche Softwares

Furthermore, you can also validate shipping zone code during the checkout in WooCommerce.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
6 Comments
Newest
Oldest
Inline Feedbacks
View all comments
hugo
3 months ago

I mean, How can I do it using a ZIP Code range?

Last edited 3 months ago by hugo
Editor
3 months ago
Reply to  hugo

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 »

hugo
3 months ago
Reply to  Saranya

You are the best, thank you.

hugo
3 months ago

How can I do it using a CEP strip?

ajay
3 months ago

It is not working for me

Editor
3 months ago
Reply to  ajay

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.

6
0
Would love your thoughts, please comment.x
()
x