WooCommerce: Disable Payment Gateway For Specific Shipping Method

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 5
 * @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() ) {
      $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®

Lifetime license, lifetime support, lifetime updates
Download as plugin, Code Snippet or raw code
Use on unlimited websites
30 days 100% money-back guarantee

FAQ & Documentation

What does this Premium Snippet® do? Where do I find the settings?

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.

What’s the difference between the plugin, Code Snippet and raw code versions?

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 🙂

Where can I get lifetime support?

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.

Where can I download my files?

Just head over to the My Account > My Downloads page. Here’s a direct link

What if a new version is released? How will I know?

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.

I don’t see any payment gateway in the drodpown?

This happens if you have no payment gateway enabled. Enable one payment method and save changes before testing again.

I’ve tried the snippet but it doesn’t suit my needs. Can I return it?

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!)

Which payment gateways is this compatible with?

ALL payment gateways that show in the default Order Review area @ WooCommerce checkout (so PayPal, Stripe, etc.).

I have a pre-sale / post-sale question?

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 🙂

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

100 thoughts on “WooCommerce: Disable Payment Gateway For Specific Shipping Method

  1. hi, how to disable payment gateway when choose a flat_rate:10?

    /**
     * @snippet       Disable Payment Gateway For Specific Shipping Method
     * @how-to        Get CustomizeWoo.com FREE
     * @author        Rodolfo Melogli
     * @testedwith    WooCommerce 5
     * @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() ) {
          $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
          $chosen_shipping = $chosen_methods[0];
          if ( isset( $available_gateways['paypal'] ) && 0 === strpos( $chosen_shipping, 'flat_rate:10' ) ) {
             unset( $available_gateways['paypal'] );
          }
       }
       return $available_gateways;
    }
    
    1. Instead of:

      0 === strpos( $chosen_shipping, 'flat_rate:10' )

      try use:

      $chosen_shipping == 'flat_rate:10'
  2. Hi,

    code works pefect, but in backen returns woocommerce error “Call to a member function get() on null in…” on line:
    “$chosen_methods = WC()->session->get( ‘chosen_shipping_methods’ );”.

    Is it possible that it is not compatible with the theme I’m using

    1. My code only runs in the frontend (!is_admin conditional). If, on the other hand, that error comes from the frontend, you may add a check:

      $chosen_methods = WC()->session && WC()->session->get( 'chosen_shipping_methods' ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
      1. it works for me, thanks!

  3. For some reason it doesnt work for me , i installed te plugin and set payment and shipping method. Nothing happens its stays availible. cleared cache also etc.

    1. Thanks for your feedback Michel! Please email me your site credentials at [email protected] and I’ll take a look. Otherwise, please email me screenshots and steps to reproduce the error. Cheers

      1. Thanks, i send you a mail

        br Michel

        1. HI all, it is working perfect now. It was my mistake 🙂 Thanks Rodolfo for quickly helping me .

            1. hi i am currently building my site and its not been transferred over to my domain yet but i am trying to disable the paypal checkout when there are no shipping options to zones out of our delivery areas. when i select the hide paypal checkout and then drop the other checkbox down it only shows my delivery zones, it doesnt show an option for no shipping options available.

              1. Hi Gary! Sorry, this is not 100% clear to me. If there are “no shipping options available”, how does WooCommerce let you complete the checkout? It should be blocked, right?

                Also, which PayPal plugin are you using, and are you referring to the PayPal “Express” buttons that display in the product, cart and checkout pages – or the default PayPal gateway option at the bottom of the checkout page?

                Thank you!

  4. I Purchased the plugin for product categories! Thank You, it works fine! BUT, it’s quite hard if there is lot of categories. It’s not working with main categories if there are sub categories under it. I have to insert rows for each sub category individualy and i have LOT of them 🙁 and after EACH row insert plugin rafresh the page, it takes long time to insert more then 100 caregoties

    1. Critical plugin error :((( after 2 hours of work inserting MULTIPLE rows for sub categories :((((

      1. Thanks so much for your valuable feedback Janis. But first, let’s fix the critical error – can you please describe the error you see in the logs so that I can send you an immediate fix? Thanks for your patience

  5. purchased the plugin, it doesn’t work. Critical error.

    1. Thanks for that Anze! Can you send me a screenshot and also the description of the error, or the steps to replicate it? Thank you!

      1. Oh, never mind, I found the bug. I will upload the revised plugin ZIP into your My Account dashboard in a few minutes. For now, if you are familiar with code, you simply need to add this line on line 16:

        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );

        Anyway, I will let you know when the new version is up. Sorry for the issue

        1. Plugin updated, you find it under My Account. Let me know if everything is ok now please.

          If you can’t remove the plugin because your site crashed and can’t access WP dashboard, go to your FTP or Hosting Control Panel > File Manager, look for /wp-content/plugins folder and delete “woocommerce-disable-payments-by-shipping” folder, then install the new version.

  6. Hey,

    It works, I didn’t expect it’s that easy. Thanks!

  7. Hello Rodolfo,

    It is possible to modify this snippet to be able to activate a payment method via coupon?

    For example. Paypal would be disabled until the usser place the “PAYPALMSI” coupon.

    1. Hey Omar, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. Hi Rodolfo,
    It works like a charm for me.
    Thank you so much!

  9. Still working fine. You always have the answer for my questions.
    Thank you so much!

  10. Hi Rodolfo, thanks for the snippet.
    I am wondering, why the

    isset( $available_gateways['cod'] )

    part in the if statement?
    Would this not work without that and just check for the shipping method?

    if (0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    unset( $available_gateways['cod'] );
    }

    Thanks again
    Cheers

    1. Ciao Luca, it would work but I first check if it exists, so that PHP errors are not generated

  11. hello,

    is there a custom code avialable where i can disable cash on delivery if coupon code is entered

    1. Hello Crusin, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  12. What a great piece of work, it works perfectly except on my health status menu I have the following error, any idea what’s happening? TIA!
    The REST API is one way WordPress, and other applications, communicate with the server. One example is the block editor screen, which relies on this to display, and save, your posts and pages.

    The REST API call gave the following unexpected result: (500) Fatal error: Uncaught Error: Call to a member function get() on null in /home/clients/c7b2592800afc300b90d509d8e57be78/web/wp-content/themes/oceanwp-child02/functions.php:65 Stack trace: #0 /home/clients/c7b2592800afc300b90d509d8e57be78/web/wp-includes/class-wp-hook.php(287): bbloomer_gateway_disable_shipping_326(Array) #1

    1. Hi Fanny, not sure, sorry

      1. I have the same problem. Otherwise the snippet works fine. Theme Astra.

  13. Works fine as of today november 2020, Great snippets!

    1. Thanks!

      1. Hi, also working for me, but I have two shipping methods and while I choose one of those need to disable one payment. So how to add one more shipping ID into snippet? Thanks

        1. Hi Migino, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  14. Hello.
    From a mobile device only, in every product, apears a button “buy now” that send the buyer to an other payment option (by woocommece) and the button “ad to basket” is underneath. I think i’ve made a conection with woo, sometime, and i want to disable it. I see no option for that in settings.
    Thanks!!!

    1. Try disabling all plugins but WooCommerce

  15. Thanks for the great snippet.
    However, it doesn’t work for me .. this is my code:

    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['benefit'] ) && 0 === strpos( $chosen_shipping, 'shipping_method_0_wbs212926e13f_smsa_express_cash_on_delivery' ) ) {
             unset( $available_gateways['benefit'] );
          }
            
       }
         
       return $available_gateways;
         
    }
    

    unlike you, i want to disable the payment methods, I have three options, cash on delivery, Benefit, and credimax
    I want to disable Benefit and credimax when choosing smsa express cash on delivery option
    but the snippet did not work for me

    1. This will require custom troubleshooting, sorry

  16. Hi Rodolfo,

    Thank you so much for your website and recommendations on WooCommerce customisation.

    I have physical and virtual products. The virtual products do not allow for a Shipping option to be set.

    Your code above though disables the payment gateway for my virtual products as well.

    Can this be remedied somehow?

    i look forward to hearing from you.

    Kind regards,

      1. Hi Rodolfo,

        I ended up purchasing “WooCommerce Conditional Payment Gateways”, which works great for many of the other scenarios I face.

        This link you shared is invaluable though. Thank you so much.

        1. Cool

  17. I just want to say thank you! Your small snippet is much better than all others plugins. It is so easy now!

    1. Thank you!

  18. Hi

    How can I check when a payment method is selected? I’m asking this question because I would like to change the button url based on the payment selected. I tried the following code but doesn’t seem to work:

    if(isset( $available_gateways['pay_by_the_month'] )) {
    		$button_html = '<a href="/contact/" title="Contact us">Enquire now</a>';
    		return $button_html;
    	}

    I also tried the following but didn’t work either:

    if($available_gateways['pay_by_the_month']->chosen == true ) {
    		$button_html = '<a href="/contact/" title="Contact us">Enquire now</a>';
    		return $button_html;
    	}

    Would you know how I can check when a payment option is selected?
    Thank you

    1. Hello Gauthier, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  19. Hello
    The code prevents contact form 7 functionning properly.

    error 500

    1. Hello Nina. Do you have a CF7 on the checkout page?

  20. please tell me, how can I disable cash on delivery when customers billing address and shipping address is different?

    1. Hey Mimo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  21. Hello,
    How can I do the following:
    I have 2 shipping methods and 2 payment methods
    When I select Cash on Delivery = it shows only specific Shipping Method
    and if I select Paypal = it shows the other Shipping Method.

    Thank you

    1. Hello Mizo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  22. Hi Rodolfo

    Is there anyway to disable specific payment gates if shipping and billing addresses are different (Billing zip code not same as shipping zip code)

    1. Alex, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  23. Big thanks for your snippet!!!!!

    1. You’re welcome!

  24. Great Thank you, I modified it to display specific payment method only if one of the shipping methods is selected and it is working fine,

    1. Great!

  25. Hi,
    I am going inform users why payment method was removed. I use woocommerce_review_order_before_payment action to display a notice when local pickup selected. Unfortunately this action runs only on initial page load. If shipping method is being changed on checkout page, notice doesn’t appear/disappear.
    It is possible to force to refresh the action?

    1. Attila, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  26. Thank you! Just what I needed and it works perfectly 😀

    1. Fantastic 🙂

  27. Looks like it doesn’t work in the newest version of Woocommerce, could you please take a look?

    1. Hi there, thanks for your comment! Just tested on Woo 3.5 and it still works 🙂

  28. Hello Rodolfo, thank you for a great work here. The snippet works wonderfully except for one shipping method. I have “easypack parcel mechines” shipping method with and without ‘COD’. When I disable ‘COD’ payment gateway for standard e.p.m it also disables COD for.. COD e.p.m. How is this possible? Shall I contact easypack parcels support or can you help me anyhow?

    All the best,
    Joanne

    1. Yes Joanne, try getting their help first 🙂 Thank you!

  29. Thanks for this, this stopped working on the latest version of woocommerce 🙁

    1. Hey Rob, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, go to WP Dashboard > WooCommerce > System Status: what errors do you see in red font?

      Also, take a look at this tutorial to see how to troubleshoot: https://businessbloomer.com/woocommerce-troubleshooting-mistakes-to-avoid/

      Finally, can you try switching temporarily to “Twentyseventeen” or “Storefront” theme and let me know if it works?

      Hope this helps!

      R

  30. Hi Rodolfo,
    your snippet works for hiding selected payment methods BUT it totally breaks the menus under Appearance → Menus screen… I tested it with Storefront on Woocommerce 3.2.6.
    It looks like the lines:
    $chosen_methods = WC()->session->get( ‘chosen_shipping_methods’ );
    $chosen_shipping = $chosen_methods[0];
    cause load-scripts.php error

    1. Thank you Anna and Natalie! Snippet has now been fixed 🙂

  31. Hi there, just got on your blog entry here, having the same issue. I pasted your code into the function on my child theme. I’m not sure if I entered the parameters correctly. I just pasted this one:

    /**
     * @snippet       Disable Payment Method for Specific Category
     * @how-to        Get CustomizeWoo.com FREE
     * @sourcecode    https://businessbloomer.com/?p=19892
     * @author        Rodolfo Melogli
     * @testedwith    WooCommerce 2.5.2
     */
     
    add_filter('woocommerce_available_payment_gateways','bbloomer_unset_gateway_by_category');
     
    function bbloomer_unset_gateway_by_category($available_gateways){
    global $woocommerce;
    $category_IDs = array(316,317);
    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    $terms = get_the_terms( $values['9'], 'Appointments' );    
    foreach ($terms as $term) {        
    if(in_array($term->term_id, $category_IDs)){
        unset( $available_gateways['cheque'] );
                break;
            }
        break;
        }
     }
        return $available_gateways;
    }
    

    Maybe I just get the ID term wrong? Any Idea?

    Thank you
    Jonathan

    1. Hey Jonathan, thanks so much for your comment! I think you left a comment on the wrong blog post, and also I don’t provide custom troubleshooting here via the blog comments I’m afraid. Little hint – check the “$values[‘product_id’]” part, you shouldn’t have a “9” in there, you should leave it as is 🙂

  32. Hi Rodolfo,
    Thanks for the great snippet! I’ve developed it a bit and created a basic plugin where you can select available payment gateways for each shipping method on woocommerce admin panel, no coding is required. Feel free to use it: https://github.com/piotr-urbanowicz/woo-payments-by-shipping

  33. How about the other way around, disabling a shipping method based on specific payment gateway?

    1. Hey Jeboy, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

    2. would be interested in that solution as well

  34. Thank you sooooo much for your tips. I do not like to install plugins for every costumization, you had helped me a lot!

  35. Thank you for this code, but on my website, it disables Menus gestion/ creation (Appearance > Menus) when i add your code, and everything is allright when i delete your code.
    My version : Woocommerce 3.1.2 ; Divi-child (Divi on last version)
    Here is my code :

     
    function bbloomer_gateway_disable_shipping_30( $available_gateways ) {
    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];
    if ( isset( $available_gateways['paypal'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    	unset( $available_gateways['paypal'] );
    }
    if ( isset( $available_gateways['cheque'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
    	unset( $available_gateways['cheque'] );
    }
    return $available_gateways;
    }
    add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_30' );
    

    Thank you for your help 🙂

    1. Nathalie, thanks so much for your comment! Unfortunately this is custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

    2. Thank you Rodolfo, I undestand, of course :).
      After contact with Divi developpers, here is the issue :

      “Your code was included on all the pages, including on the dashboard, and for some reason, WooCommerce variables weren’t loaded there, on the menu page, and it was returning a fatal error.
      So I added a conditional check to include the code just on front-end and it seems to work fine” :

      if(!is_admin() ) add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_gateway_disable_shipping_30' );

      I don’t know why, but it works !

      1. Good to know, thanks a lot 🙂

    3. Thanks Nathalie for your snippet, it resolved my issue as well 🙂

  36. Hello..

    not working with wc 3.0. Changed local_delivery name local_pickup but not working.

    1. Thanks Bulent! I just added a new version of the snippet, let me know if it works 🙂

  37. Hi Rodolfo.
    I succeeded with your snippet for disabling cod for product class. Thanks for that. This one is a bit puzzle.
    I have a shipping method Česká pošta (czech post). I found its id=16. What should I put instead of your ‘local_delivery’ to the snippet?
    I tried the name ‘Česká pošta’ ,but it does not work. Could I use the ID somehow?
    thanks
    Lasercat

    1. Hey Zdenek, thanks for your comment! Go to WooCommerce > Settings > Shipping > Shipping Zone > Shipping Method and see where the Shipping ID is by using “Inspect Element” in Chrome. I believe it will be something like “ceska-posta” or “ceska_posta” 🙂

  38. WooCommerce 2.6.4 The code does not work ((Please, update!! 🙂
    I get an error 500 when I go to the /wp-admin

    1. Hey Voltik, thanks for your comment! I pasted this function in my test website and it doesn’t give me error at all – can you please check again? Then we will go through the fact that WooCommerce changed how the shipping works in 2.6, but for now at least let me know if it doesn’t break your theme! 🙂

  39. Hi Rudolfo,

    I’ve only just encountered this code and it seems to be very close to what I’m after.

    With our site though, the customer can’t choose their shipping method. Instead, it is set based on their country. How would I alter your code to look at the shipping method, without first checking to see if it has been chosen?

    I hope that was clear!

    1. Hello Mark, thanks for your comment 🙂 WooCommerce provides already a geolocation functionality – you could find out how that works and use that instead of using conditional logic by shipping method! Hope this helps a little 🙂

  40. Hi Rudolfo,

    Nice one, can you put an example with two gateways and two shipping methods

    🙂 thanks

    1. Hey Lubo thanks for your comment! It’s quite simple actually – under this code:

      if ( isset( $available_gateways['cod'] ) && $chosen_shipping == 'local_delivery' ) {
      unset( $available_gateways['cod'] );
      }
      

      you can add another if statement:

      if ( isset( $available_gateways['cod'] ) && $chosen_shipping == 'local_delivery' ) {
      unset( $available_gateways['cod'] );
      }
      if ( isset( $available_gateways['paypal'] ) && $chosen_shipping == 'flat_rate' ) {
      unset( $available_gateways['paypal'] );
      }
      

      Hope this helps 🙂

      1. Hi Rodolfo !
        Thank you for your snippet.
        Everything is working fine but when I choose another shipping method the filter does not apply any more (cod comes back). Do you have also this issue with storefront ?
        Woocommerce version 2.6.14

        1. Hey Marc thanks for your comment! I’m not sure I fully understand your point – this snippet disables a gateway only for 1 specific shipping method, so if you change shipping the gateway should come back. Let me know!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.