Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Edgar Rasquin 326 posts 925 karma points
    May 26, 2022 @ 08:25
    Edgar Rasquin
    0

    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 anyone give me some advice?

    Thank you very much and best regards, Edgar

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    May 26, 2022 @ 08:35
    Matt Brailsford
    0

    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.

  • Edgar Rasquin 326 posts 925 karma points
    May 26, 2022 @ 08:49
    Edgar Rasquin
    0
    [HttpPost]
        public ActionResult AddToCart(AddToCartDto postModel)
        {
            try
            {
                using (var uow = _unitOfWorkProvider.Create())
                {
                    var store = CurrentPage.GetStore();
                    var order = _sessionManager.GetOrCreateCurrentOrder(store.Id)
                        .AsWritable(uow)
                        .AddProduct(postModel.ProductReference, postModel.ProductVariantReference, postModel.Quantity);
    
                    var productExists = order.OrderLines.Where(x => x.ProductReference == postModel.ProductReference);
    
                    if (!productExists.Any())
                    {
                        _orderService.SaveOrder(order);
                        uow.Complete();
                    }
    
                    _orderService.SaveOrder(order);
                    uow.Complete();
                }
            }
            catch(ValidationException ex)
            {
                ModelState.AddModelError("productReference", "Failed to add Product to cart");
                return CurrentUmbracoPage();
            }
    
            TempData["addedProductReference"] = postModel.ProductReference;
    
            return RedirectToCurrentUmbracoPage();
        }
    
  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    May 26, 2022 @ 09:01
    Matt Brailsford
    100

    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.

Please Sign in or register to post replies

Write your reply to:

Draft