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 7
 * @community     https://businessbloomer.com/club/
 */
 
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 🙂

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]

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. Follow @rmelogli

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

  1. Dear Rodolfo,
    I have used this code for years on, and it worked well.
    Thank you very much for your work!
    Now, with the new Woo 7.7.2 and WordPress 6.2.2 it just doesn’t.
    Actually it just starts, and than the cart is not loading, the website health tool shows that there’s a Rest API problem. As I remove the code from the functions.php all is nice, and working well…
    What should get wrong?

    1. Thanks for that Katalin. This checks if WC()->session exists before trying to get the current shipping method so that error makes not much sense to me. Are you using the exact snippet as per this page?

  2. Hi Rudolfo, Do you have any tips how to handle the WooCommerce PayPal Payments module “ppcp-gateway”? It loads the PP button as an iframe at page load which is hidden if the module is not selected. Scenario: PP is disabled for USA. Customer with country USA comes to checkout, PP module is hidden. Customer changes country to UK, PP is shown, but payment button is missing as the JS that loads the button wasn’t triggered on page load. If the customer refreshes the page, the button appears. I’m trying to find a solution for this, but hitting a brick wall.

    1. Hello Edith! Did you talk to the plugin developers already?

  3. Hi, the code on PHP 8.0 causes an error:
    Warning: Trying to access array offset on value of type null in…/functions.php on line 33

    line 33:

    $chosen_shipping = $chosen_methods[0];

    Are you also having this error? For me it appears on the login page – /wp-admin

    I think the following code will fix it:

          if ( ! empty( $chosen_methods ) && isset( $chosen_methods[0] ) ) {
       $chosen_shipping = $chosen_methods[0];

    Regards

      1. Pin this addition at start.
        It saved me, and since the the php is going to 8+ i belive it should be mentioned.
        Many thanks again 🙂

        1. Thank you! How do I “pin” a comment?

  4. Is there an plugin conflict with the latest woocommerce 7.0.0 . it stoped wokrking after the update

    1. Hi Michel, and sorry for the delay! I haven’t tested it with Woo 7 yet, but I don’t see why it won’t work. Have you tried with just 2022 theme + Woo + the plugin active only?

  5. 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
     * @community     https://businessbloomer.com/club/
     */
      
    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'
  6. 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!

        1. I had the same problem with API calls. The snippet worked well, but API response returned an error.
          This solution solved my issue too!
          Thanks

  7. 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 rodolfo@businessbloomer.com 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!

  8. 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

  9. 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.

  10. Hey,

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

  11. 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!

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

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

  14. 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

  15. 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!

  16. 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.

  17. 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!

  18. 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

  19. 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

  20. 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

  21. 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!

  22. 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!

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

    error 500

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

  24. 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!

  25. 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!

  26. 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!

  27. Big thanks for your snippet!!!!!

    1. You’re welcome!

  28. 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!

  29. 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!

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

    1. Fantastic 🙂

  31. 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 🙂

  32. 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!

  33. 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

  34. 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 🙂

  35. 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 🙂

  36. 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

  37. 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

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

  39. 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 🙂

  40. 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 🙂

  41. 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” 🙂

  42. 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! 🙂

  43. 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 🙂

  44. 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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *