Esempio n. 1
0
        public ManagerResponse <CartResult, bool> AddLineItemsToCart(string userId, IEnumerable <AddCartLineInputModel> inputModelList)
        {
            Assert.ArgumentNotNull(inputModelList, nameof(inputModelList));

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

            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, bool>(cartResult, cartResult.Success));
            }

            var lines = new List <CartLine>();

            foreach (var inputModel in inputModelList)
            {
                Assert.ArgumentNotNullOrEmpty(inputModel.ProductId, nameof(inputModel.ProductId));
                Assert.ArgumentNotNullOrEmpty(inputModel.CatalogName, nameof(inputModel.CatalogName));
                Assert.ArgumentNotNull(inputModel.Quantity, nameof(inputModel.Quantity));

                if (inputModel.Quantity == null)
                {
                    continue;
                }
                var quantity = (uint)inputModel.Quantity;

                var cartLine = new CommerceCartLine(inputModel.CatalogName, inputModel.ProductId, inputModel.VariantId == "-1" ? null : inputModel.VariantId, quantity)
                {
                    Properties =
                    {
                        ["ProductUrl"] = inputModel.ProductUrl,
                        ["ImageUrl"]   = inputModel.ImageUrl
                    }
                };
                //UpdateStockInformation(cartLine, inputModel.CatalogName);

                lines.Add(cartLine);
            }

            CartCacheHelper.InvalidateCartCache(userId);

            var cart            = cartResult.Cart as CommerceCart;
            var addLinesRequest = new AddCartLinesRequest(cart, lines);

            RefreshCart(addLinesRequest, true);
            var addLinesResult = CartServiceProvider.AddCartLines(addLinesRequest);

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

            AddBasketErrorsToResult(addLinesResult.Cart as CommerceCart, addLinesResult);

            addLinesResult.WriteToSitecoreLog();
            return(new ManagerResponse <CartResult, bool>(addLinesResult, addLinesResult.Success));
        }
Esempio n. 2
0
        public ManagerResponse <RemovePromoCodeResult, CommerceCart> RemovePromoCodeFromCart(string userId, string promoCode)
        {
            Assert.ArgumentNotNullOrEmpty(promoCode, nameof(promoCode));

            var result = new RemovePromoCodeResult {
                Success = false
            };
            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 <RemovePromoCodeResult, CommerceCart>(result, cartResult.Cart as CommerceCart));
            }

            var cart = cartResult.Cart as CommerceCart;

            CartCacheHelper.InvalidateCartCache(userId);

            var request = new RemovePromoCodeRequest(cart, promoCode);

            RefreshCart(request, true); // We need the CS pipelines to run.
            result = ((CommerceCartServiceProvider)CartServiceProvider).RemovePromoCode(request);
            if (result.Success && result.Cart != null)
            {
                CartCacheHelper.AddCartToCache(result.Cart as CommerceCart);
            }

            result.WriteToSitecoreLog();
            return(new ManagerResponse <RemovePromoCodeResult, CommerceCart>(result, result.Cart as CommerceCart));
        }
        public JsonResult GetCurrentCart()
        {
            try
            {
                var          id   = CommerceUserContext.Current.UserId;
                var          cart = CartCacheHelper.GetCart(id);
                CartApiModel cartResult;

                // The following condition stops the creation of empty carts on startup.
                if (cart == null && CartCookieHelper.DoesCookieExistForCustomer(id))
                {
                    var response = CartManager.GetCart(CommerceUserContext.Current.UserId, true);
                    cartResult = new CartApiModel(response.ServiceProviderResult);
                    if (response.ServiceProviderResult.Success && response.Result != null)
                    {
                        cartResult.Initialize(response.ServiceProviderResult.Cart);
                        if (response.ServiceProviderResult.Cart != null)
                        {
                            CartCacheHelper.AddCartToCache(response.ServiceProviderResult.Cart as CommerceCart);
                        }
                    }
                }
                else
                {
                    cartResult = new CartApiModel();
                    cartResult.Initialize(cart);
                }

                return(Json(cartResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("GetCurrentCart", e), JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
        public ManagerResponse <AddShippingInfoResult, CommerceCart> SetShippingMethods(string userId, SetShippingMethodsInputModel inputModel)
        {
            Assert.ArgumentNotNull(inputModel, nameof(inputModel));

            var result = new AddShippingInfoResult {
                Success = false
            };
            var response = GetCart(userId, true);

            if (!response.ServiceProviderResult.Success || response.Result == null)
            {
                return(new ManagerResponse <AddShippingInfoResult, CommerceCart>(result, response.Result));
            }

            var cart = (CommerceCart)response.ServiceProviderResult.Cart;

            if (inputModel.ShippingAddresses != null && inputModel.ShippingAddresses.Any())
            {
                var cartParties = cart.Parties.ToList();
                cartParties.AddRange(inputModel.ShippingAddresses.Select(item => item.ToParty()).ToList());
                cart.Parties = cartParties.AsReadOnly();
            }

            var internalShippingList = inputModel.ShippingMethods.Select(item => item.ToShippingInfo()).ToList();
            var orderPreferenceType  = InputModelExtension.GetShippingOptionType(inputModel.OrderShippingPreferenceType);

            if (orderPreferenceType != ShippingOptionType.DeliverItemsIndividually)
            {
                foreach (var shipping in internalShippingList)
                {
                    shipping.LineIDs = (from CommerceCartLine lineItem in cart.Lines select lineItem.ExternalCartLineId).ToList().AsReadOnly();
                }
            }

            CartCacheHelper.InvalidateCartCache(userId);

            result = AddShippingInfoToCart(cart, orderPreferenceType, internalShippingList);
            if (result.Success && result.Cart != null)
            {
                CartCacheHelper.AddCartToCache(result.Cart as CommerceCart);
            }

            return(new ManagerResponse <AddShippingInfoResult, CommerceCart>(result, result.Cart as CommerceCart));
        }
Esempio n. 6
0
        public ManagerResponse <CartResult, CommerceCart> MergeCarts(string newUserId, string previousUserId, Cart anonymousVisitorCart)
        {
            Assert.ArgumentNotNullOrEmpty(previousUserId, nameof(previousUserId));
            Assert.ArgumentNotNullOrEmpty(newUserId, nameof(newUserId));

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

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

            var currentCart = (CommerceCart)cartResult.Cart;
            var result      = new CartResult {
                Cart = currentCart, Success = true
            };

            if (newUserId != previousUserId)
            {
                var anonymousCartHasPromocodes = anonymousVisitorCart is CommerceCart &&
                                                 ((CommerceCart)anonymousVisitorCart).OrderForms.Any(of => of.PromoCodes.Any());

                if (anonymousVisitorCart != null && (anonymousVisitorCart.Lines.Any() || anonymousCartHasPromocodes))
                {
                    if (currentCart.ShopName == anonymousVisitorCart.ShopName || currentCart.ExternalId != anonymousVisitorCart.ExternalId)
                    {
                        var mergeCartRequest = new MergeCartRequest(anonymousVisitorCart, currentCart);
                        result = CartServiceProvider.MergeCart(mergeCartRequest);
                    }
                }
            }

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

            return(new ManagerResponse <CartResult, CommerceCart>(result, result.Cart as CommerceCart));
        }
Esempio n. 7
0
        public ManagerResponse <CartResult, CommerceCart> RemoveLineItemFromCart(string userId, string externalCartLineId)
        {
            Assert.ArgumentNotNullOrEmpty(externalCartLineId, nameof(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 as CommerceCart;
            var lineToRemove = cart.Lines.SingleOrDefault(cl => cl.ExternalCartLineId == externalCartLineId);

            if (lineToRemove == null)
            {
                return(new ManagerResponse <CartResult, CommerceCart>(new CartResult {
                    Success = true
                }, cart));
            }

            var removeLinesRequest = new RemoveCartLinesRequest(cart, new[] { lineToRemove });

            RefreshCart(removeLinesRequest, true);
            var removeLinesResult = CartServiceProvider.RemoveCartLines(removeLinesRequest);

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

            AddBasketErrorsToResult(removeLinesResult.Cart as CommerceCart, removeLinesResult);

            removeLinesResult.WriteToSitecoreLog();
            return(new ManagerResponse <CartResult, CommerceCart>(removeLinesResult, removeLinesResult.Cart as CommerceCart));
        }
        public ManagerResponse <CartResult, CommerceCart> GetCart(string userId, bool refresh = false)
        {
            if (refresh)
            {
                CartCacheHelper.InvalidateCartCache(userId);
            }

            var cart = CartCacheHelper.GetCart(userId);

            if (cart != null)
            {
                var result = new CartResult {
                    Cart = cart
                };
                AddBasketErrorsToResult(result.Cart as CommerceCart, result);
                return(new ManagerResponse <CartResult, CommerceCart>(result, cart));
            }

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

            if (cartResult.Success && cartResult.Cart != null)
            {
                cart            = cartResult.Cart as CommerceCart;
                cartResult.Cart = cart;
                CartCacheHelper.AddCartToCache(cart);
            }
            else
            {
                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
                });
            }

            AddBasketErrorsToResult(cartResult.Cart as CommerceCart, cartResult);

            return(new ManagerResponse <CartResult, CommerceCart>(cartResult, cart));
        }
Esempio n. 9
0
 public void AddToCart(CommerceCart cartLine)
 {
     _cartCacheHelper.AddCartToCache(cartLine);
 }