Limit Quantity When Adding a Product to Cart

Limiting how many items of a product a user can purchase using Drupal Commerce can be a challenge. There are several ways to accomplish this, but here’s our favorite.

We're going to create a Rule that ensures a user cannot purchase more than 1 item of a certain product in our store. This is the best way to ensure that product can never have an invalid quantity.

1. Create a rule at admin/config/workflow/rules/reaction/add.

2. We'll name the Rule "Limit Quantity to 1"

3. Set the React on event option to Commerce Line Item (Before saving a commerce line item).

4. Your rule reaction should look like this:

Rule Reaction

5. Click "Save".

6. Since we want to modify a "Product" line item, we need to ensure the Line Item is of type: product. Click "add condition". The condition we need is "Entity is of bundle". The Entity we are checking is the "commerce-line-item". Click "Continue" to now select the bundle. Now select "Product" as the bundle.

7. Next, we want to enable the quantity limit only on a specific product. We are going to check the product ID of the product that is in our line item. Click "add condition". Select "data comparison". The first item to compare is the product ID "commerce-line-item:commerce-product:product-id". Click "Continue". Next we want to ensure the ID is equal to "10". Enter "10" as the second value to compare and click "Save".

8. It's action time! We need to ensure the quantity is always 1 for this product. Click "add action" and we want to "set a data value".

9. The data value we are updating is "commerce-line-item:quantity".

10. Set the value to 1.

11. Now we need to save the updated entity. Click "add action".

12. The action we need is "Save entity".

13. The entity is "commerce-line-item". Click "Save".

That's it, you're done! A user will be unable to purchase more than 1 item of "Product 10".

The final Rule should look something like:
Rule Final 1