How to Hide Payment Method in Woocommerce based on Postal Code

In this woocommerce setup, I have 2 Payment methods, Paypal and Cash on Delivery.

Now how can Cash on Delivery be hidden/disabled for certain Postal codes only.

Read More

This is the code I found on Gist

//  Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
    global $woocommerce;
    if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
        unset(  $available_gateways['ccavenue'] );
    } else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
        unset( $available_gateways['paypal'] );
    }
    return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

Gist Link

Related posts

Leave a Reply

3 comments

  1. To disable/hidden “Cash on Delivery”, Place this code in your theme’s function.php .

    For more detail: woocommerce-hide-payment-gatway-based-on-visitors-country

    //  Disable gateway based on country
    function payment_gateway_disable_country( $available_gateways ) {
    global $woocommerce;
    if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
        unset(  $available_gateways['cod'] );
    }
    return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
    
  2. In the “checkout page” user can have two addresses – billing and shipping one.

    To work correctly only with changes of Shipping one if it’s filled I changed a bit the code. You have to test shipping countrycode if it’s set, if not just user countrycode:

    function payment_gateway_disable_country( $available_gateways ) {
        global $woocommerce;
        $country = !empty($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->customer->get_country();
        if ( isset( $available_gateways['cod'] ) && $country <> 'CZ' ) {
            unset(  $available_gateways['cod'] );
        }
        return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
    
  3. In the code above you used country code to disable payment gateway by, but you mentioned you would like to do it by Postal Code.

    You’re right about using woocommerce_available_payment_gateways, but instead of using $woocommerce->customer->get_country() you have to use WC()->customer->get_shipping_postcode() (or WC()->customer->get_billing_postcode() for some situations).

    You mentioned PayPal and Cash on Delivery payment gateway, we need their IDs, there are paypal and cod accordingly.

    In the code below let’s deactivate Cash on Delivery for a couple postal codes, for example ‘1234’ and ‘5678’:

    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['cod'];
        }
    
        // get postal code
        $postal_code = WC()->customer->get_billing_postcode();
    
        // deactivate payment method
        if( in_array( $postal_code, array( '1234', '5678' ) ) ) {
            unset(  $available_gateways['cod'] );
        }
            
        return $available_gateways;
    
    } );
    

    Code can be inserted to your current theme functions.php file or a custom plugin. More info you can find in this tutorial: https://rudrastyh.com/woocommerce/hide-payment-methods-based-on-postal-code.html