Menu Close

WooCommerce: Disable Payment Gateway For Specific Shipping Method

default

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!

WooCommerce: disable gateway based on shipping method
WooCommerce: disable gateway based on shipping method

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 CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_for_shipping_rate' );
 
function bbloomer_gateway_disable_for_shipping_rate( $available_gateways ) {
   if ( ! is_admin() && WC()->session ) {
      $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;
}

Mini-Plugin: Business Bloomer WooCommerce Toggle Payments By Shipping

You don’t feel confident with coding? You need more control over your payment/shipping exclusions? You don’t want to purchase yet another bloated, expensive plugin? Great!

Business Bloomer WooCommerce Toggle Payments By Shipping is a mini WooCommerce plugin, without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. A single and easy admin dashboard.

Screenshot of the settings:

Quick demo:

As you can see the settings are pretty straight forward. Select a payment method you wish to hide/show from the left, and the shipping method that should trigger that from the right. Add more rules if needed. Simple!

Advanced Plugin: WooCommerce Conditional Payment Gateways

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 🙂

View Source
Posted in WooCommerce Tips