Disable payment gateways based on country from woocommerce

Disable payment gateways based on country is a task that must do any commerce to sell in many countries. How we know, Woocommerce has fields like country or state in his checkout form and those will help us to do this task.

Before, we have to know the short name of each gateway to we can play with them. Here a table with the gateways more know.

 

Short Code Name Long
cod Cash on Delivery
bacs Direct bank transfer
cheque Check payments
paypal Paypal
stripe Stripe
fullculqi Culqi
payme Payme

 

Now we know about those short names, we can remove or add it how we want. So, we will use the woocommerce_available_payment_gateways  hook to do it.

 

Here are 2 examples of: Disable payment gateways based on country from woocommerce

Example 1: If the country is AR, the we will remove the Paypal Gateway and if the country is the US, we will remove Culqi and Payme Gateway.

<?php
function letsgo_remove_gateways($gateways = []) {

	$country = WC()->customer->get_billing_country();

	switch($country) {
		case 'AR' : unset( $gateways['paypal'] ); break;
		case 'US' : 
			unset($gateways['fullculqi']);
			unset($gateways['payme']);
			break;
	}
	
	return $gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'letsgo_remove_gateways', 10, 1);

 

Example 2: If the country is the US and the state is Florida ( FL ), use Paypal as the only payment gateway

<?php
function letsgo_add_gateways($gateways = []) {

	$country = WC()->customer->get_billing_country();
	$state = WC()->customer->get_billing_state();
	$aux_gateways = $gateways;

	if( $country == 'US' && $state == 'FL' ) {
		foreach( $aux_gateways as $key => $data ) {
			if( $key == 'paypal' )
				continue;

			unset($gateways[$key]);
		}
	}
	
	return $gateways;
}

add_filter( 'woocommerce_available_payment_gateways', 'letsgo_add_gateways', 10, 1);

 

Remember that you must put this code in your functions.php on your active theme.

 

 

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jim Grant

Hi,
Will this work where the PayPal checkout button is on the Cart page? I ask because I have been told that having the button there bypasses the Woo address fields.
To help understand the blocking process, does the code work on the data provided directly from PP. Also, does it work if the buyer pays by bank card?
Thanks

Back to Top
2
0
Would love your thoughts, please comment.x
()
x