Hide a Payment Method for Certain Countries
Removes a payment method from checkout when the customer's billing country is on your list, for example to offer cash on delivery only at home.
Opens WPIDE on
Code
add_filter( 'woocommerce_available_payment_gateways', function ( $gateways ) {
$gateway = 'cod'; // Payment method ID to hide.
$countries = array( 'CA', 'MX' ); // Billing countries where it is hidden.
if ( is_admin() || ! WC()->customer || ! isset( $gateways[ $gateway ] ) ) {
return $gateways;
}
if ( in_array( WC()->customer->get_billing_country(), $countries, true ) ) {
unset( $gateways[ $gateway ] );
}
return $gateways;
} );
Notes
Set $gateway to the payment method ID and list two-letter country codes in $countries.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.