I want to limit the number of products that can be added to the cart from a particular category.I want the customer to add only one product from each category. If the customer tries to add one more product ,the product added previously should get replaced by the one added later. Can anyone help me with this?

This is my Observer.php code. enter image description here

  • do you also want to restrict on no of quantity of a product as well or just on no of product ? – Ashish Madankar M2 Professiona May 25 '16 at 13:20
  • On both no of quantity as well as no of products. – sk1 May 26 '16 at 7:26

I can help you with steps you need to take:

  1. Identify an event you can hook onto using an observer. This event should be one that always fires when you add-to-cart but before the product is actually added to cart. This is because you need to validate the product being added to the existing cart, if there's is one.
  2. Identify what category(ies) it belongs to and record it.
  3. Check the quote's items and get all of their categories as well.
  4. Validate that the product's category(ies) against the quote's categories.
  5. Take corresponding measures.
| improve this answer | |
  • foe this you can use controller_action_predispatch_checkout_cart_add this event – Ashish Madankar M2 Professiona May 26 '16 at 8:37
  • Can you please elaborate a little more on how to proceed with this event? – sk1 May 26 '16 at 10:57
  • This worked fine. Thank you so much for help! – sk1 May 27 '16 at 12:30

In my opinion, the best way is using a Magento extension to set up a required order quantity for products in a category that customers have to meet before checking out.

I found out an extension on Magento Connect that supports this function: Limit Order Quantity per Category


| improve this answer | |
  • But I think that won't replace the product that was already added with the selected one. – sk1 May 26 '16 at 10:33

Three events used during Add To Cart Process

2) sales_quote_product_add_after


Use oberserver event is :

1) sale_quote_add_item

I will send you code later

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.