Woocommerce: Disable Payment Gateway Based On Country
If you web store is doing business in multiple countries, some payment gateway might not available in certain countries. Thus, for better checkout experience, remove the unavailable payment gateway in customer country could be the good idea.
Here is how you can do it without plugin.
add_filter( 'woocommerce_available_payment_gateways', 'remove_payment_gateway' );
function remove_payment_gateway( $available_gateways ){
if ( is_admin() ) $available_gateways; //make sure only works in frontend.
if( null !== WC()->customer ){ //if customer inserted the address.
$geo = new WC_Geolocation();
if($geo->geolocate_ip()['country'] == 'Singapore' || $geo->geolocate_ip()['country'] == 'SG' ){ //if the customer billing is from singapore
unset( $available_gateways['paypal'] ); // paypal is the payment gateway id
}
}
return $available_gateways;
}
I am using woocommerce_available_payment_gateways
filter hook to return the desired payment gateways.
To obtain Payment Gateway ID , you can goto Wocommerce -> Settings -> Payments Here is where you can enable or disable as well as configure the Payment gateways. Click on the gateways that you want to get the Gateway ID, the ID will be in the URL.
For example, https://yoursite/com/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=paypal
In the section=paypal
part, it is the Gateway ID you want to use here.
Note that this example only show how you can choose the desire gateways in the checkout, you can use this technique for other condition as well, like gateway base on products, or other customer meta data.