WooCommerce: Only Allow 1 Product in the Cart

Here’s how to limit your WooCommerce Cart to just one product at a time.

This simple PHP snippet can be used for many applications. For example, your store may only allow to buy one subscription at a time. On this same website, on the other hand, customers can only purchase one product at a time so it’s easier for me to manage invoicing and payments, given that I switch PayPal and Stripe accounts based on what’s inside the cart.

Here’s the quick fix – enjoy!

WooCommerce: only allow 1 product in the cart

PHP Snippet: Limit the Cart to Max One Product – WooCommerce

/**
 * @snippet       WooCommerce Max 1 Product @ Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WC 5.1
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 9999, 2 );
  
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {
   wc_empty_cart();
   return $passed;
}

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.

107 thoughts on “WooCommerce: Only Allow 1 Product in the Cart

  1. Not Working, I try to deactivate all plugins but nothing,

    1. Hi there, it works on this same website, so it must be something else!

  2. Hi, it stopped working after one of the latest updates 🙁

    1. Not possible. Please do the usual troubleshooting: turn off all plugins but Woo, switch theme, and test again.

  3. Ciao Rodolfo,

    FYI – I just found out that this snippet caused an issue with YITH plugin “Request a Quote”.
    When the client accepts the quote, it’s redirected to the checkout page and after completion a new order is created (instead of completing the “quote order”).
    I have no idea why this happens, but removing the snippet solved.

    Grazie!

    1. Fair enough, thank you!

  4. Thank you works perfect!!!!!!!!!! Made my day 🙂

  5. how to apply it on a specific subcategory

    1. Hi Ambuj I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂

  6. Thanks a lot! This code really helped me a lot and saved time and headaches!

  7. its still working! thank you so much you save me with so much trouble with your code. thank you so so so much!

  8. Thank You!
    Since Store Customizer didn’t work for this I lost all Hope
    Thanks to You for being very helpful.

  9. Hi, Is there a way to edit the warning message that comes up when people try to add more than one product into the basket.

    1. Hello Jack, 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!

      1. Hi Rodolfo, did you find the solution to this issue: how to hide cart alerts in checkout page, because when we come again to the page the alert says that is not possibile to add more items ? Thanks

  10. This looks really good but having tried it out it didn’t quite achieve what I wanted i.e.
    1. Customer adds product A to the basket and it shows there – great.
    2. Customer adds product B to the basket and in order to show just one item, product A is removed and substituted by product B
    3. Customer adds product C to the basket which substitutes product B etc etc
    I want the customer to add product A to the basket and then HAVE to checkout without being able to add/substiture more products. Can this be achieved?

    1. Neill, 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!

  11. Perfect man! inspired me to do the ajax call to update order review after add_to_cart_button on one_page_checkout!

    1. Cool!

  12. I would like to restrict this option for category level . Any Advice.

    1. Kumar, 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!

  13. hello, is there any option to sell specific product once in month for every user.
    like i am offering membership for health checkup and 1 checkup is free in a month so if any user order 2nd time in a month then they unable to order and see a warning like you can buy this once in a month only,
    only on specific product.

    1. Hi Sanket, 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. Hellooo!
    Thank you
    this works well! 🙂

    1. Cool!

  15. Still works! Thank you so much for this!

    1. Great!

  16. Great! it works fine.

    Thank You Rodolfo Melogi!!!

    1. Thank you!

  17. For some of my products it doesn’t work and just hides the add to cart button.

    1. There must be something else. This only triggers when you actually add to cart

  18. Perfect. Thanks!

    1. Cool!

  19. It’s working

    Thanks Man
    Have a good day

    1. Cool

  20. Can this snippet be applied to one certain product only, and not the others in the shop?

    1. Hello Sabrina, 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. This is fantastic – good for a quick fix. Is there a simple way to apply this to just variations so only one is allowed?

    1. Ryan, 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. Hello!

    This works great. But there is a situation in which a second product can be added to the basket:
    The user previously added something to his cart. Then he enters the site without authorization, adds something else to the basket, only then he logs in and .. We have 2 products in basket – old product + new 🙂

    1. Then he enters the site without authorization

      What does that mean? 🙂

      1. Hey Rodolfo, Vladimir is correct. Here are the steps to reproduce:

        (1) Log into the site as a customer.
        (2) Add a product to your cart.
        (3) Log out of the site (now your cart will appear as “empty”).
        (4) Add a different product to your cart.
        (5) During the checkout process, log into the site.
        (6) Your cart has two items in it, and both can be bought simultaneously.

        While this seems to be an unusual sequence of steps, it’s not so unusual in the wild. This is an issue I’ve been encountering with my users fairly frequently, and they’ve been able to purchase multiple items at once.

        1. I can’t replicate this:

          (3) Log out of the site (now your cart will appear as “empty”).

          Am I missing something? Cart stays full if I log out

          1. That’s strange. Logging out totally clears the cart for me as well — we must have some sort of difference in configuration. I am running WordPress 5.2.3 and WooCommerce 3.7.0.

            1. Can you test with no plugins but WooCommerce?

              1. Hi Rodolfo

                I have the same problem.
                Notably, I use a snippet to redirect to checkout when the user clicks on the add to cart button.

                User enters the site, adds a product to cart and proceeds to checkout. But he does not complete the purchase and leaves the site. ( he is logged out automatically)

                User comes back to site after a few hours and adds a new product to cart and proceed to checkout.
                But this time, the previous product is also in the checkout.

                1. Since I don’t show the cart page to customers, I decided to remove the cart in every product page.
                  I find the following code for this purpose. In the initial tests, it seems fine.
                  What do you think of this snippet?

                       add_action( 'template_redirect', 'custom_empty_cart' );
                  function custom_empty_cart() {
                      if ( is_product() && ! ( is_cart() || is_checkout() ) ) {
                          WC()->cart->empty_cart( true );
                  }
                  }
                  
                    1. Hi
                      I have the same problem with double product in cart if user logged in or add product with login.
                      Any solution on this?

  23. Works like a charm. Many thanks to you – appreciate very much your sharing. This snippet is gold.
    (WP 5.2.2, WooC Version 3.6.5, PHP 7.3.8 on Apache)

    1. Great!

  24. can we add a notification that 1 product is allowed?

    1. Hello Henk, 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. Fantastic content!! thanks.

    1. Thanks!

  26. You are my man!

    1. Thank you!

  27. Hii Sir

    You have a way to limit these 12 products in the cart?

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

  28. Hello master!

    I’m using a form plugin to add a product to the cart, then the form redirects me to a checkout page with the added product.

    I’ve inserted your code in my function.php but it doesn’t works for me, it adds a new one every time I complete the form.

    Is it possible to get some help? I can provide to you a URL to test!

    Thanks!!!!

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

  29. Thanks for sharing this code, very helpful.

    1. Great 🙂

  30. Awsome, works fine. Thank you so much

    1. Cool 🙂

  31. Is it possible to allow a buyer to never be able to buy the same product again?

    1. Hello Bruno, 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. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  32. Great little snippet. Can this be modified within the same hook so that not only does only 1 item get added to the cart but also the QTY remains at 1?

    1. Hey Blue, thanks for your comment! You can force the quantity to 1 on the Edit Product page – under Product Data 🙂

  33. just added your snippet on a woocommerce website for a cleint, works fine! Thanks a lot!

  34. Hi!
    Is it possible to ignore this function if I’m adding just a variant of the SAME product?
    With this code, if I add Product A variant 1, and then later I add SAME product but variant 2, I’ll have only the second one in the cart. What I need is just to exclude products, no matter quantity and variations, so if I add Product A can never also add Product B or C, but can add som other variations of Product A
    Thanks!

    1. Hey Domenico, 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

  35. Dear Sir,

    I have add this code working fine but some time user have 2 product added for cart different categories product.

    how to do user any time any one category product added in cart not multiple category.

    1. Hi there, thanks so much for your comment! This snippet won’t consider the category – it applies to all products no matter what. Hope this helps 🙂

  36. You are a life saver!
    I’ve been trying for hours and everybody is calling the filter with priority (10 , 5) only when I used your (99, 2) numbers did it work.
    Thanks a million 🙂

  37. Hi I just implemented this and it worked great but I need to limit it to categories and also need it to work with woomobify.
    Can you do this?
    Thanks
    Colin

    1. Hey Colin, thanks so much for your comment! Yes, this is possible – if you’re interested feel free to post your job specifications via the Contact page and I’ll get back to you as soon as I can. Thank you 🙂

  38. Hi Rodolpho
    thank you very much for that snippet, sadly it won’t work on my site (WP 4.9.2 & WooC 3.2.6)
    Cheers C

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

      To troubleshoot, 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

  39. Do you have a way to limit this to only 2 products in the cart?

    1. Robin, 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

  40. Hi Rodolfo

    its not working for me, because its add more then one . i want user buy only one product at a time

    1. Poras, thanks so much for your comment! It works on my test site, so I think you might have a plugin conflict or theme error.

      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 to “Twentyseventeen” theme and let me know if it works with that default theme?

      Hope this helps!

      R

  41. Hi,

    Great article! Is there a way we can hide the notification “You reached the maximum amount of items in your cart.”?

    1. Anshul, 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

  42. Awesome stuff, Rodolfo!!
    Worked like a charm, thank you 😀

  43. Hi Rodolfo,

    Thanks for your efforts to make such blog & article. Its really helping us to customize WooCommerce. And the code snippet is also working fine for me.
    Can we add a custom notification that “At One time only one products can be added to cart “

    1. Rajan, thanks for your comment! I have updated the snippet based on your request 🙂 Cheers!

  44. Hello, it’s a great article but I have a question: how can I limit one product by brand in the cart, using your logic this ?? I would like to do this?

    thank you
    Kiko – direct from Brazil

    1. Hello Kiko, thanks for your comment! Take a look at this tutorial: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/ and you should find a “conditional” rule to apply this function only if your product belongs to a category. Best of luck 🙂

  45. Thank you for nice code snippets.
    I’m wondering if this could be modified to do the following:
    If product ID is any of X,X,X and any of those is already in cart, any of those will not be allowed to added again, instead shows error message instead of emptying cart.
    This way customer could buy only one of those limited products, but could also buy other not limited products at the same time.
    Would be very useful in certain situations.

    1. Hey Mika, thanks so much for your feedback and comment!

      Yes, this is possible of course – but unfortunately this is custom development work and I cannot provide this solution on the blog right now. I will add it to my to-write list but I cannot guarantee I will be publishing this before 2-3 months.

      If you would like to get a quote for the fix, feel free to go here to get in touch.

      Thanks for your understanding!

      ~R

  46. works perfect…!!!!

  47. Is there a way I can apply this to one specific product only, not globally? If other products are already in cart, cart needs to be emptied and no further products allowed once this specific product is in the cart?

    1. Of course Louise 🙂 Try using this (second) edit:

      // Only one product in cart
      
      add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 10, 2 );
       
      function bbloomer_only_one_in_cart( $cart_item_data, $product_id ) {
      global $woocommerce;
      // if adding product ID, empty cart
      
      if ( $product_id == 851 ) {
      $woocommerce->cart->empty_cart();
      } else {
      
        // if adding other products, check ID is not @ cart already
        foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if( $_product->id == 851 ) {
              wc_add_notice('That product is already in the cart!', 'error' );
      	return false;
        }
       }
      }
      return $cart_item_data;
      }
      
      
      1. Hi Rodolfo,

        Unfortunately that didn’t work.

        The product was added to the cart and all other products remained in there. By the way, there are TWO products for which I want this to happen, ID 12251 and 12246.

        Can’t understand why it didn’t work – it looks so straight-forward….

        1. I think you’re right, this snippet empties the cart if we add the product to cart, but does not deny adding more products to cart if the product is already in there. I’ll see if I can revise it 🙂

  48. This little snippet is very snappy. Thanks for sharing.

    1. Thanks a lot for your feedback Chris!

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. Required fields are marked *