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)); }
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 ManagerResponse <CartResult, bool> UpdateCartCurrency(string userId, string currencyCode) { Assert.ArgumentNotNullOrEmpty(currencyCode, nameof(currencyCode)); var result = GetCart(userId); if (!result.ServiceProviderResult.Success) { return(new ManagerResponse <CartResult, bool>(new CartResult { Success = false }, false)); } var cart = result.Result; var changes = new CommerceCart { CurrencyCode = currencyCode }; var updateCartResult = UpdateCart(cart, changes); if (updateCartResult.ServiceProviderResult.Success) { CartCacheHelper.InvalidateCartCache(userId); } return(new ManagerResponse <CartResult, bool>(updateCartResult.ServiceProviderResult, updateCartResult.ServiceProviderResult.Success)); }
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)); } }
public ManagerResponse <SubmitVisitorOrderResult, CommerceOrder> SubmitVisitorOrder(string userId, SubmitOrderInputModel inputModel) { Assert.ArgumentNotNull(inputModel, nameof(inputModel)); var errorResult = new SubmitVisitorOrderResult { Success = false }; var response = CartManager.GetCart(userId, true); if (!response.ServiceProviderResult.Success || response.Result == null) { response.ServiceProviderResult.SystemMessages.ToList().ForEach(m => errorResult.SystemMessages.Add(m)); return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null)); } var cart = (CommerceCart)response.ServiceProviderResult.Cart; if (cart.Lines.Count == 0) { errorResult.SystemMessages.Add(new SystemMessage { Message = DictionaryPhraseRepository.Current.Get("/System Messages/Orders/Submit Order Has Empty Cart", "Cannot submit and order with an empty cart.") }); return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, null)); } cart.Email = inputModel.UserEmail; var request = new SubmitVisitorOrderRequest(cart); RefreshCartOnOrdersRequest(request); errorResult = OrderServiceProvider.SubmitVisitorOrder(request); if (errorResult.Success && errorResult.Order != null && errorResult.CartWithErrors == null) { CartCacheHelper.InvalidateCartCache(userId); try { var wasEmailSent = MailManager.SendMail("PurchaseConfirmation", inputModel.UserEmail, string.Join(" ", cart.Parties.FirstOrDefault()?.FirstName, cart.Parties.FirstOrDefault()?.LastName), errorResult.Order.TrackingNumber, errorResult.Order.OrderDate, string.Join(", ", cart.Lines.Select(x => x.Product.ProductName)), cart.Total.Amount.ToCurrency(cart.Total.CurrencyCode)); if (!wasEmailSent) { var message = DictionaryPhraseRepository.Current.Get("/System Messages/Orders/Could Not Send Email Error", "Sorry, the email could not be sent"); //errorResult.SystemMessages.Add(new SystemMessage(message)); } } catch (Exception ex) { Log.Error("Could not send Purchase Confirmation mail message", ex, this); var message = DictionaryPhraseRepository.Current.Get("/System Messages/Orders/Could Not Send Email Error", "Sorry, the email could not be sent"); errorResult.SystemMessages.Add(new SystemMessage(message)); } } errorResult.WriteToSitecoreLog(); return(new ManagerResponse <SubmitVisitorOrderResult, CommerceOrder>(errorResult, errorResult.Order as CommerceOrder)); }
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)); }
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)); }
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)); }
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)); }
public void Invalidate(string customerId) { _cartCacheHelper.InvalidateCartCache(customerId); }