Skip to content
Advertisement

Only allow to purchase one Item In Woocommerce 3

Is there anyway to prevent more than one item for purchase in WooCommerce, or prevent more than one item added to the cart?

I have different products but I want to allow only one item per checkout.

I tried to search for solution but those existing solutions are not working properly, let’s say when user is not signed in and adds an item into cart and then goes to checkout and logins there an item that has been added previously when customer was logged in also adds up to the one customer just added, so now there is 2 products inside the cart, and this is an issue here is the code that I’m using that’s not working properly.

JavaScript

Advertisement

Answer

Updated (with a 2nd alternative as asked in your comment).

The code below will limit add to cart to a unique item displaying an error message when adding more than one. A second function will check cart items avoiding checkout and adding an error message when there is more than one item:

JavaScript

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


1) When trying add to cart a 2nd item:

enter image description here

2) If a more than one item is in cart:

enter image description here

3) And in checkout you will get an empty page with the same error notice:

enter image description here


To allow only one cart item removing any additional items that will work in any cases:

JavaScript

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement