How do I prevent a product from ending up twice in the shopping basket?
Hello,
I am planning a site for a sports club. There, the member can book a membership or register for a course.
Membership and course are the products in this scenario.
Now I have the problem that I have to prevent a product from being added to the shopping cart if it is already in the shopping cart.
I have already tried to add a query in the AddToCart action and only save the order if there is no OrderLine with the corresponding ProductReference in the order yet:
var productExists = order.OrderLines.Where(x => x.ProductReference == postModel.ProductReference);
if (!productExists.Any())
{
_orderService.SaveOrder(order);
uow.Complete();
}
Although there are no error messages in debug mode, the code does not seem to work in this form.
Can you share all your code for the AddToCart method? I've got the feeling that you could be adding the product to the cart and then checking it's existence which will always be true at that point and so it never saves.
Yup, so you add the product and then check to see if an order line exists with that product and so your check will always be true and save will never be triggered.
You should check for the order line existence BEFORE you add the product to the order again.
How do I prevent a product from ending up twice in the shopping basket?
Hello,
I am planning a site for a sports club. There, the member can book a membership or register for a course. Membership and course are the products in this scenario. Now I have the problem that I have to prevent a product from being added to the shopping cart if it is already in the shopping cart.
I have already tried to add a query in the AddToCart action and only save the order if there is no OrderLine with the corresponding ProductReference in the order yet:
Although there are no error messages in debug mode, the code does not seem to work in this form.
Can anyone give me some advice?
Thank you very much and best regards, Edgar
Hi Edgar,
Can you share all your code for the AddToCart method? I've got the feeling that you could be adding the product to the cart and then checking it's existence which will always be true at that point and so it never saves.
Yup, so you add the product and then check to see if an order line exists with that product and so your check will always be
true
and save will never be triggered.You should check for the order line existence BEFORE you add the product to the order again.
is working on a reply...