Today we take a look at the WooCommerce Checkout Page and specifically at how to disable a payment gateway (for example PayPal) when a specific shipping method is selected (e.g. “local_pickup”).
Specifically, you will learn how to “get” the selected shipping method on the go (thanks to “sessions”), and also how to “unset” a payment gateway. Enjoy!
PHP Snippet: Disable Payment Gateway For Specific Shipping Method – WooCommerce
In this example, I will disable “COD” payment gateway for all “local pickup” shipping rates in whatever shipping zone. You can also target a specific shipping rate (in a single zone).
/**
* @snippet Disable Payment Gateway For Specific Shipping Method
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 5
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_available_payment_gateways', 'tutoraspire_gateway_disable_for_shipping_rate' );
function tutoraspire_gateway_disable_for_shipping_rate( $available_gateways ) {
if ( ! is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
Business Bloomer Premium Snippet®: WooCommerce Disable Payments By Shipping
You don’t feel confident with coding? Check.
You need more control over multiple payment/shipping rates exclusions? Check.
You don’t want to purchase yet another bloated, expensive plugin? Check.
Business Bloomer’s WooCommerce Disable Payments By Shipping Premium Snippet® is a mini WooCommerce plugin, without the usual hassles. Lifetime license. Just a few lines of code. Use it on as many websites as you like. Get lifetime support and documentation. A single and super-easy admin dashboard. Download as a plugin, as a Code Snippet or as raw PHP code.
Screenshot? Here you go:
As you can see Business Bloomer’s WooCommerce Disable Payments By Shipping Premium Snippet® settings are pretty straight forward. Select a payment method you wish to hide from the left, and the shipping rate that should trigger that from the right. Add more rules if needed. Simple!
Interested?
WooCommerce Disable Payments By Shipping – Premium Snippet®
FAQ & Documentation
It adds a new table under WooCommerce > Settings > Payments where you can define which payment method gets disabled at Checkout if a certain shipping rate is selected.
There is no difference, code is the exact same. And you will get access to all of them, so that you can pick whatever version suits you best. Want to use it as a plugin? Install it as a normal plugin via the supplied ZIP. Prefer using Code Snippets plugin? Cool, simply import the provided JSON snippet! Familiar with code? Great, easily place the raw code into your child theme’s functions.php file. Just pick one 🙂
On this same page. Simply click here to scroll down to the comments area. If you use the same email under your customer account, your comment will receive priority.
Just head over to the My Account > My Downloads page. Here’s a direct link
As long as you don’t unsubscribe from Business Bloomer email list, you will receive an email once a new update is available. You can then go to the Downloads page and find the latest version.
This happens if you have no payment gateway enabled. Enable one payment method and save changes before testing again.
Sure, and you get 100% money back if you request a refund within 30 days from your purchase. As of now there is no automated system on the My Account > My Orders page, so drop me an email and I’ll process your refund. Working on it though (expect an article on BB soon!)
ALL payment gateways that show in the default Order Review area @ WooCommerce checkout (so PayPal, Stripe, etc.).
Cool! Post your request now and I’ll do my best to get back to you asap
Is There a More Advanced WooCommerce “Payment Gateways by Shipping Method” Plugin?
If you require a more advanced premium plugin, I decided to look for a reliable plugin that achieves the same result of this snippet (and more).
In this case, I found the WooCommerce Conditional Payment Gateways plugin to be the most complete when you need to enable/disable payment gateways based on certain criteria. You can create unlimited “rules” and use, for example, cart totals, billing country, shipping country, user role and much more to define which payment gateway shows and which not.
But in case you don’t want to use plugins and wish to code (or wish to try that), then keep reading 🙂