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”). Enjoy!

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

PHP Snippet: Disable Payment Gateway For Specific Shipping Method – WooCommerce

/**
 * @snippet       Disable Payment Gateway For Specific Shipping Method
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.6.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_326' );
 
function bbloomer_gateway_disable_shipping_326( $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;
    
}

Is There a WooCommerce “Payment Gateways by Shipping Method” Plugin?

If you don’t feel 100% confident with coding, 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 🙂

Share:

Leave a Reply