What can we help you with?
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 the 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 ) { // Not in backend (admin) and Not in order pay page if( is_admin() || is_wc_endpoint_url('order-pay') ) return $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.
For example, when the country is the US or any other country, you can see that the payment gateways are listed on 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 the 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 on the product documentation page.
2 Comments. Leave new
If your wp-admin page doesn’t load, or you get an error by adding the above code (which happened with mine) add the following code after the 2nd line. It will fix 🤗
// Not in backend (admin) and Not in order pay page
if( is_admin() || is_wc_endpoint_url(‘order-pay’) )
return $available_gateways;
Hello, Thanks for pointing out the error. We have updated the article.