In my website I would like to limit product quantity to 1, meaning is that user can order only one of a product in one order. And my question is how to configure to set maximum quantity to 1 . Any suggestion is greatly appreciated.
Thanks.
You can achieve this using Rules by following steps given below:
Event: After udpdating an already existing line item
Conditions: 1/ data comparison: commerce-line-item:type = product 2/ data comparison: commerce-line-item:commerce-product:type = Your given product type 3/ data comparison: commerce-line-item:quantity is greater than 1
Action: - set data value: commerce-line-item:quantity 1 - display custom message
Reference: https://drupalcommerce.org/questions/8066/how-limit-maximum-quantity-1-one-given-product-type
I found this link, it is very simple. http://bonify.io/blog/2014/09/limit-quantity-when-adding-product-cart
condition
Entity is of bundle
data selector: commerce-line-item, value: product
Data comparison
Data to compare : commerce-line-item:commerce-product:product-id, operator: equal, Value:1
Action
set a data value
data: commerce-line-item:quantity, value: 1
Save Entity
Data selector: commerce-line-item,
Show a message on the site
Message: This product only available for 1., Value: warning
October 2022: Running Commerce Core on Drupal 7
So I struggled with this. On my website we sell digital products so the quantity never needed to be larger than one. I tried using spinners to prevent double clicking on the "Add to Cart" buttons but somehow users still found ways to double click. I still don't know how but they did. I started my solution from trying to follow the drupal commerce user guide.
https://docs.drupalcommerce.org/commerce1/user-guide/shopping-cart/working-with-cart-rules-events
The problem I ran into was that when I tried to set the condition to
commerce-line-item:quantity > 1 it tried to force me to use data selectors which doesn't give me any value option.
The only thing I could do was set
commerce-line-item:quantity = 2 and then the routine would work. But if someone triple and went really crazy with the clicks this would not work because the QTY would get to three before the rule would finish firing. I then tried adding more conditions in an OR statement but found the performance of the rule was unreliable even in non stress situations. THEN THE EUREKA MOMENT.
I didn't need any condition since everything on my website is to be set to 1. So no product type conditions, no pre-existing quantity conditions, etc... needed to be checked.
So here is what appears to be working for me. I'll come back and edit this if it fails at any point.
Comments: On my website you must be logged in to make a purchase. It appears to work for regular registered users and the admin role. I'll come back and update if I have any new problems with this running. What this routine does is make sure that after the user has finally stopped clicking the "Add to Cart" button, line item quantity is set back to 1 and the cart is recalculated. I imagine it increases the "Add to Cart" time a little bit. But it should be worth it not to have calls from people that didn't realize they had overpaid because they added the product to the cart twice. I'm a bit surprised but it happens all the time. Especially on my website cause when I was on drupal 6 I used a different commerce solution and forcing a quantity of 1 was already coded in. So many customers never bothered to check.