Posted on

WooCommerce: Only Allow 1 Product in the Cart

Here’s how to limit your WooCommerce Cart to just 1 product. This simple PHP snippet can be used for many applications – for example, your store only allows to buy one product at a time, or max one subscription. Here’s the quick fix – just copy/paste in your functions/php!

WooCommerce: only allow 1 product in the cart

PHP snippet: allow only one product to cart in WooCommerce


/**
 * @snippet       WooCommerce Only one product in cart
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=560
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.4.3
 */

add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_only_one_in_cart', 99, 2 );
 
function bbloomer_only_one_in_cart( $passed, $added_product_id ) {

// empty cart first: new item will replace previous
wc_empty_cart();

// display a message if you like
wc_add_notice( 'New product added to cart!', 'notice' );

return $passed;
}

Where to add this code?

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

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.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

  1. Awsome, works fine. Thank you so much

    1. Cool 🙂

  2. […] @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055  * @sourcecode    https://businessbloomer.com/?p=560  * @author        Rodolfo Melogli  * @testedwith    WooCommerce 3.4.3  */ add_filter( […]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  18. works perfect…!!!!

    1. Thank you Rajesh! 🙂

  19. […] friend Rodolfo, from BusinessBloomer, posted a solution that he adapted from his solution to allow only one product to the cart. It works, but in our opinion, that approach presented a few […]

  20. 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;
      }
      
      
    2. 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….

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

  21. 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're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.