Example #1
0
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.CartManager.ChangeLineQuantity(CurrentStorefront, CurrentVisitorContext, inputModel);
                var result   = new AXCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("UpdateLineItem", this, e);
                return(Json(new BaseJsonResult("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, nameof(inputModel));

                var validationResult = this.CreateJsonResult();
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = CartManager.ChangeLineQuantity(CommerceUserContext.Current.UserId, inputModel);
                var result   = new CartApiModel(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);

                    if (HasBasketErrors(response.Result))
                    {
                        // We clear the cart from the cache when basket errors are detected.  This stops the message from being displayed over and over as the
                        // cart will be retrieved again from CS and the pipelines will be executed.
                        CartCacheHelper.InvalidateCartCache(CommerceUserContext.Current.UserId);
                    }
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }
Example #3
0
        public CSCartBaseJsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            var response = _cartManager.ChangeLineQuantity(CurrentStorefront, CurrentVisitorContext, inputModel);
            var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                result.Initialize(response.Result, _productResolver);

                if (response.Result.HasBasketErrors())
                {
                    // We clear the cart from the cache when basket errors are detected.  This stops the message from being displayed over and over as the
                    // cart will be retrieved again from CS and the pipelines will be executed.
                    _cartCacheService.Invalidate(CurrentVisitorContext.GetCustomerId());
                }
            }

            return(result);
        }
Example #4
0
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");
                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);

                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var result = _cartRepository.UpdateLineItem(inputModel);

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                _logger.LogError("UpdateLineItem", this, e);
                return(Json(new BaseJsonResult("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }
Example #5
0
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.CartManager.ChangeLineQuantity(CurrentStorefront, CurrentVisitorContext, inputModel);
                var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);

                    if (response.Result.HasBasketErrors())
                    {
                        // We clear the cart from the cache when basket errors are detected.  This stops the message from being displayed over and over as the
                        // cart will be retrieved again from CS and the pipelines will be executed.
                        var cartCache = CommerceTypeLoader.CreateInstance <CartCacheHelper>();
                        cartCache.InvalidateCartCache(this.CurrentVisitorContext.GetCustomerId());
                    }
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("UpdateLineItem", this, e);
                return(Json(new BaseJsonResult("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }
Example #6
0
        public ManagerResponse <CartResult, CommerceCart> ChangeLineQuantity(string userId, UpdateCartLineInputModel inputModel)
        {
            Assert.ArgumentNotNull(inputModel, nameof(inputModel));
            Assert.ArgumentNotNullOrEmpty(inputModel.ExternalCartLineId, nameof(inputModel.ExternalCartLineId));

            var cartResult = LoadCartByName(CommerceConstants.CartSettings.DefaultCartName, userId);

            if (!cartResult.Success || cartResult.Cart == null)
            {
                var message = DictionaryPhraseRepository.Current.Get("/System Messages/Cart/Cart Not Found Error", "Could not retrieve the cart for the current user");
                cartResult.SystemMessages.Add(new SystemMessage {
                    Message = message
                });
                return(new ManagerResponse <CartResult, CommerceCart>(cartResult, cartResult.Cart as CommerceCart));
            }

            CartCacheHelper.InvalidateCartCache(userId);

            var cart   = cartResult.Cart;
            var result = new CartResult {
                Cart = cart, Success = true
            };
            var cartLineToChange = cart.Lines.SingleOrDefault(cl => cl.Product != null && cl.ExternalCartLineId == inputModel.ExternalCartLineId);

            if (inputModel.Quantity == 0 && cartLineToChange != null)
            {
                result = RemoveCartLines(cart, new[] { cartLineToChange }, true);
            }
            else if (cartLineToChange != null)
            {
                cartLineToChange.Quantity = inputModel.Quantity;
                var request = new UpdateCartLinesRequest(cart, new[] { cartLineToChange });
                RefreshCart(request, true);
                result = CartServiceProvider.UpdateCartLines(request);
            }

            if (result.Success && result.Cart != null)
            {
                CartCacheHelper.AddCartToCache(result.Cart as CommerceCart);
            }

            AddBasketErrorsToResult(result.Cart as CommerceCart, result);

            return(new ManagerResponse <CartResult, CommerceCart>(result, result.Cart as CommerceCart));
        }
        /// <summary>
        /// Changes the line quantity.
        /// </summary>
        /// <param name="storefront">The storefront.</param>
        /// <param name="visitorContext">The visitor context.</param>
        /// <param name="inputModel">The input model.</param>
        /// <returns>
        /// The manager response where the modified CommerceCart is returned in the Result.
        /// </returns>
        public virtual ManagerResponse <CartResult, CommerceCart> ChangeLineQuantity([NotNull] CommerceStorefront storefront, [NotNull] VisitorContext visitorContext, [NotNull] UpdateCartLineInputModel inputModel)
        {
            Assert.ArgumentNotNull(inputModel, "inputModel");
            Assert.ArgumentNotNullOrEmpty(inputModel.ExternalCartLineId, "inputModel.ExternalCartLineId");

            var cartResult = this.LoadCartByName(storefront.ShopName, storefront.DefaultCartName, visitorContext.UserId);

            if (!cartResult.Success || cartResult.Cart == null)
            {
                var message = StorefrontManager.GetSystemMessage(StorefrontConstants.SystemMessages.CartNotFoundError);
                cartResult.SystemMessages.Add(new SystemMessage
                {
                    Message = message
                });
                return(new ManagerResponse <CartResult, CommerceCart>(cartResult, cartResult.Cart as CommerceCart));
            }

            var cartCache = CommerceTypeLoader.CreateInstance <CartCacheHelper>();

            cartCache.InvalidateCartCache(visitorContext.GetCustomerId());

            var cart   = cartResult.Cart;
            var result = new CartResult {
                Cart = cart, Success = true
            };
            var cartLineToChange = cart.Lines.SingleOrDefault(cl => cl.Product != null && cl.ExternalCartLineId == inputModel.ExternalCartLineId);

            if (inputModel.Quantity == 0 && cartLineToChange != null)
            {
                result = this.RemoveCartLines(cart, new[] { cartLineToChange }, true);
            }
            else if (cartLineToChange != null)
            {
                cartLineToChange.Quantity = inputModel.Quantity;
                var request = new UpdateCartLinesRequest(cart, new[] { cartLineToChange });
                request.RefreshCart(true);
                result = this.CartServiceProvider.UpdateCartLines(request);
            }

            if (result.Success && result.Cart != null)
            {
                cartCache.AddCartToCache(result.Cart as CommerceCart);
            }

            return(new ManagerResponse <CartResult, CommerceCart>(result, result.Cart as CommerceCart));
        }
 public ManagerResponse <CartResult, CommerceCart> ChangeLineQuantity(CommerceStorefront storefront, IVisitorContext visitorContext, UpdateCartLineInputModel inputModel)
 {
     return(null);
 }