Menu Close

Disable Payment Gateway for a Specific Country

Codeamend

Sometimes you might need to disable PayPal for non-local customers or enable a specific gateway for only one country. Either way, this is a very common requirement for all of those who trade internationally.

Here’s a simple PHP snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable (“paypal”, “authorize”, “stripe”, etc.), use the country code (US, ES, IE, etc.) and then apply your conditional rules in the function.php or new plugin.

How to Disable/Enable specific Gateway for specific Billing Country
add_filter( 'woocommerce_available_payment_gateways', codeamend_payment_gateway_disable_country);
  
function codeamend_payment_gateway_disable_country( $available_gateways ) {
    if ( is_admin() ) return $available_gateways;
    if ( isset( $available_gateways['authorize'] ) && WC()->customer->get_billing_country() <> 'US' ) {
        unset( $available_gateways['authorize'] );
    } else {
        if ( isset( $available_gateways['paypal'] ) && WC()->customer->get_billing_country() == 'US' ) {
            unset( $available_gateways['paypal'] );
        }
    }
    return $available_gateways;
}
 

Note to find payment gateway slug: When you hover the payment gateway name in Woocommerce payment settings you can find refer: https://example.com/wp-admin/admin.php?page=wc-settings&tab=checkout&section=bacs

Posted in WooCommerce

You can also read...