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
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x