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 solution can be used for many applications. For example, your store may only allow to buy one subscription at a time.

On this same website, for example, 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

Note: technically speaking, the code below will force the product added to cart to replace whatever is in the cart, hence will only allow one product in the cart.

/**
 * @snippet       WooCommerce Max 1 Product @ Cart
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WCooCommerce 8
 * @community     https://businessbloomer.com/club/
 */
 
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 9999 );
  
function bbloomer_only_one_in_cart( $passed ) {
   wc_empty_cart();
   return $passed;
}

Mini-Plugin: Business Bloomer WooCommerce Only Allow 1 Product in the Cart

You don’t feel confident with coding? You don’t want to purchase yet another bloated, expensive plugin? Great!

Business Bloomer WooCommerce Only Allow 1 Product in the Cart 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. Super simple admin dashboard.

Screenshot of the settings:

Quick demo:

As you can see the settings are pretty straight forward. Select the add to cart behavior from the dropdown, and the error message in case you choose the “Deny” behavior. Simple!

Advanced Plugin: WooCommerce Quantity Manager

WooCommerce Quantity Manager is a fully featured plugin and lets you add a range of quantity rules, including allowing only 1 product in the cart.

To do this, you would set it up with a maximum order quantity of 1. For more fine-grained control, you can also set the maximum of each product, or the maximum number of products from a specific category, which can be in the cart.

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: How to Fix the “Cart is Empty” Issue
    For some reason, sometimes you add products to cart but the cart page stays empty (even if you can clearly see the cart widget has products in it for example). But don’t worry – it may just be a simple cache issue (and if you don’t know what cache is that’s no problem either) or […]
  • 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: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]

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

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

  1. Not working. Was working. Tried using it on a fresh install on localhost as well with only woo and wp code snippets active; still not working.

    1. Works on this same website under certain conditions, so it must be something else. To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/

      Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

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

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

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

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

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

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

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

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

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

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

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

    1. Cool!

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

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

  15. Hellooo!
    Thank you
    this works well! 🙂

    1. Cool!

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

    1. Great!

  17. Great! it works fine.

    Thank You Rodolfo Melogi!!!

    1. Thank you!

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

  19. Perfect. Thanks!

    1. Cool!

  20. It’s working

    Thanks Man
    Have a good day

    1. Cool

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

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

  23. 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?

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

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

  26. Fantastic content!! thanks.

    1. Thanks!

  27. You are my man!

    1. Thank you!

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

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

  30. Thanks for sharing this code, very helpful.

    1. Great 🙂

  31. Awsome, works fine. Thank you so much

    1. Cool 🙂

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  47. works perfect…!!!!

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

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

    1. Thanks a lot for your feedback Chris!

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 *