How to restrict one product per category per cart?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Hi,

We have a need to restrict only one product per cart if the product belongs to "certain restricted categories", which means they can add as many products in to the cart from other categories. Please note this is not the restriction on the quantity, but the product itself.

If the user wants to buy "another product" from the same category of those "certain restricted categories", s/he needs to complete this purchase first and add that "another product" to cart to complete the purchase separately.

Hope this makes sense. We did look for existing plugins, but could not find any. Would appreciate if anyone can point us to an existing plugin or in the right direction where to change the code.
3 years ago
I dont know about existing plugins
To customise you would need to modify the add to cart code
Have a look at \Presentation\Nop.Web\Views\Product\_AddToCart.cshtml
There is an ajax call to AddProductToCart-Details which is routed to
AddProductToCart_Details in the ShoppingCart Controller
\Presentation\Nop.Web\Controllers\ShoppingCartController.cs
To do it in a plugin you will need to override or action alternate methods
3 years ago
The Shipping Director plugin can do it (see example in blog).  However, the message only appears in the checkout at the shipping method selection page (not when item is added to cart).
3 years ago
Thank you Yidna and New York.

New York, I like your solution in the sense that one can build dynamic conditions to stop customers from checking out. But unfortunately I don't think it will work for us for the reasons stated below:

The products in question are "downloadable products" and so no shipping required. Even if we dodge the product settings to enable shipping for a downloadable product and tick "Free Shipping", the expression you have given in your example would probably work for "columns in the Product" table, but our requirement is to make it work based on categories. Unless you guide me otherwise, I assume the Shipping Director's expression cannot look into the link table (Product_Category_Mapping). Even if we make it to look into the link table, we don't want to implement this condition for all categories (as explained in my original post), it is only for "certain restricted categories", so basically we need an AND condition in your expression builder.

We are not plugin developers (yet), so we are going to look into customising the code Yidna suggested and see if we can build our bespoke conditions.

Once again, thank you for your inputs.
3 years ago
Shipping Director has many built-in variables, properties and extension methods, and you can use operators like 'and', 'or', etc.

So, for example, you could write a Boolean expression like...
RE: "restrict only one product per cart if the product belongs to "certain restricted categories"
Items.Count(Product.HasCategory("Restricted")) > 1

or if you are concerned with Quantity (an "Item" in the shopping cart has a Quantity)
Items.Where(Product.HasCategory("Restricted")).Sum(Quantity) > 1

I don't see that your scenario requires an "AND", but as an example, if your restriction only applied when the Country was not the UK, then it could be:

Country <> 'GB and Items.Count(Product.HasCategory("Restricted")) > 1
3 years ago
Thank you New York, this is really helpful. Let me take a closer look at the link you sent and the examples you gave. Much appreciated.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.