How to Hide WooCommerce Payment Methods based on Country for Free using a Simple Code Snippet?

Here is a quick and easy option to hide WooCommerce payment method based on the country for free using a simple code snippet.

Currently, there is no better way than using a WooCommerce conditional payment gateway code snippet to hide the WooCommerce payment method other than this. For this, you need to navigate to the functions.php file and then place this code.

add_filter( 'woocommerce_available_payment_gateways', 'elex_manage_payment_gateway' );
function elex_manage_payment_gateway( $available_gateways ) {
$new_payment_gateways = array();
if ( WC()->customer->get_shipping_country() == 'IN' ) {
$new_payment_gateways['razorpay'] = $available_gateways['razorpay'];
} else {
unset ($available_gateways['razorpay']);
$new_payment_gateways = $available_gateways;
}
return $new_payment_gateways;
}

The above WooCommerce conditional payment gateway code snippet, upon applying hides other WooCommerce payment gateways, when the Country is selected as IN i.e India by the customer and then shows only Razor Pay in the payment options.

Like for example when the country is the US or any other country, you can see that the payment gateways are listed in the checkout page.

When the country is changed to India, then the payment methods are changed to RazorPay and also a GST is added to the payment.

If you want to add an extra cost with the WooCommerce payment methods like GST option to the Payment method in India, you can use below code snippet.

add_action( 'woocommerce_cart_calculate_fees','elex_add_gst_fee' );
function elex_add_gst_fee($cart) {
if(is_checkout() && WC()->customer->get_shipping_country() == 'IN') {
$fee = (18/100) * $cart->cart_contents_total;
$cart->add_fee( 'GST (18%)', $fee );
}
}

To Wrap up,

You can always make changes to the code snippet as per your requirement. Like hiding the WooCommercce payment gateways, you can also hide WooCommerce shipping methods depending upon various factors like country, category and much more using the  ELEX Hide WooCommerce Shipping Methods plugin.

If you are looking for WooCommerce payment plugins to integrate with your WooCommerce store, then you can include these popular WooCommerce payment plugins on your WooCommerce store without much hassle.

Both these payment plugins offer a secured and smooth payment gateway for your customers. Read more about the same in the product documentation page.

    Leave a Reply

    WordPress Image Lightbox Plugin