//[ValidateAntiForgeryToken] public ActionResult UpdateLineItem(UpdateLineItemModel updateModel) { if (ModelState.IsValid) { try { var cart = updateModel.Quantity == 0 ? _cartApi.DeleteLineItemAsync(updateModel.LineItemId).Result : _cartApi.UpdateLineItemQuantityAsync(updateModel.LineItemId, updateModel.Quantity).Result; WebSession.Current.SetPersistentProperty(WebSession.ShoppingCartCount, cart.TotalItemsInCart.ToString(CultureInfo.InvariantCulture)); if (updateModel.Quantity == 0) { return(Redirect(_linkGenerator.GenerateShoppingCartLink())); } return(PartialView("ShoppingCart", _shoppingCartViewModelBuilder.GetShoppingCartViewModelAsync().Result)); } catch (ShopperApiException exc) { var model = _shoppingCartViewModelBuilder.GetShoppingCartViewModelAsync().Result; model.ErrorMessages = new[] { exc.ShopperApiError.Description }; return(PartialView("ShoppingCart", model)); } catch (Exception) { var model = _shoppingCartViewModelBuilder.GetShoppingCartViewModelAsync().Result; model.ErrorMessages = new[] { Res.ShoppingCart_UpdateLineItemFailed }; return(PartialView("ShoppingCart", model)); } } var vm = _shoppingCartViewModelBuilder.GetShoppingCartViewModelAsync().Result; vm.ErrorMessages = InternalGetErrorMessages(ModelState).ToArray(); return(PartialView("ShoppingCart", vm)); }
private MiniCartViewModel GetViewModel() { var model = new MiniCartViewModel(); var quantityProperty = WebSession.Current.GetPersistentProperty(WebSession.ShoppingCartCount); if (!String.IsNullOrEmpty(quantityProperty)) { model.Quantity = Convert.ToInt32(quantityProperty); } else { var cart = _cartApi.GetCartAsync().Result; if (cart != null) { model.Quantity = cart.TotalItemsInCart; } WebSession.Current.SetPersistentProperty(WebSession.ShoppingCartCount, model.Quantity.ToString(CultureInfo.InvariantCulture)); } SiteInfo siteInfo; if (WebSession.Current.TryGetSiteInfo(out siteInfo) && siteInfo.IsCartHandledBySite) { model.IsCartHandledBySite = true; model.CartLink = _linkGenerator.GenerateShoppingCartLink(); } else { model.IsCartHandledBySite = false; model.CartLink = siteInfo.DrShoppingCartUrl; } return(model); }
private string MakeAddToCartLink(long productId, long offerId) { var link = _linkGenerator.GenerateShoppingCartLink() ?? ""; if (String.IsNullOrEmpty(link)) { return(link); } if (!link.EndsWith("/")) { link += "/"; } var json = "[{\"ProductId\":\"" + productId + "\",\"Quantity\":1,\"OfferId\":\"" + offerId + "\"}]"; link += "addtocart?products=" + HttpUtility.UrlEncode(json); return(link); }
public async Task <CrossSellViewModel> GetCrossSellViewModelAsync(string promotionId, string shoppingCartLink) { var pop = await _offersApi.GetOffersAsync(promotionId).ConfigureAwait(false); return(MakeCrossSellViewModel(pop, _linkGenerator.GenerateShoppingCartLink())); }
public override ActionResult Index() { CrossSellViewModel vm = null; var promotionId = CurrentItem.PromotionId; int?maxNumberOfProducts = 0; // We are on a shopping cart page. Try to build the cross-sell data with the items in the cart. string emptyCartPromotionId = null; if (CurrentItem != null) { emptyCartPromotionId = CurrentItem.EmptyCartPromotionId; maxNumberOfProducts = CurrentItem.MaxNumberOfProducts; } //todo: Get the cart from session?? var cartViewModel = WebSession.Current.Get <ShoppingCartViewModel>(WebSession.ShoppingCartSlot); var cart = cartViewModel == null?_cartApi.GetCartAsync().Result : cartViewModel.Cart; if (cart.LineItems.LineItem != null) { foreach (var li in cart.LineItems.LineItem) { if (li.Product != null) { vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, li.Product, _linkGenerator.GenerateShoppingCartLink()).Result; } if (vm != null) { break; } } } // If nothing is in the cart, or if no Offers were returned for this product, try to use the default offers. if (vm == null || vm.Offers == null || vm.Offers.Count == 0) { vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, _linkGenerator.GenerateShoppingCartLink()).Result; } // Set the empty cart promotion Id, which is used by an Ajax call to the ShoppingCartPart.GetUpdatedCrossSell() if (vm != null) { vm.EmptyCartPromotionId = emptyCartPromotionId; } OffersViewModelBuilder.RemoveCartItemsFromOffers(vm, cart); if (vm == null) { vm = new CrossSellViewModel { Offers = new List <CrossSellOfferViewModel>() } } ; vm.Name = CurrentItem.Name; vm.Title = CurrentItem.Title; vm.PromotionId = promotionId; vm.MaxNumberOfProducts = maxNumberOfProducts ?? 0; return(PartialView(vm)); } }
public override ActionResult Index() { CrossSellViewModel vm = null; var promotionId = CurrentItem.PromotionId; int?maxNumberOfProducts = 0; if (CurrentItem is CrossSellInterstitialPart) { var siteRoot = CmsFinder.FindSitePageFromSiteId(WebSession.Current.SiteId); if (siteRoot != null && !string.IsNullOrEmpty(siteRoot.PromotionId)) { promotionId = siteRoot.PromotionId; } } if (CurrentPage is ShoppingCartPage) { // We are on a shopping cart page. Try to build the cross-sell data with the items in the cart. string emptyCartPromotionId = null; var part = CurrentItem as CandyRackPart; if (part != null) { emptyCartPromotionId = part.EmptyCartPromotionId; maxNumberOfProducts = part.MaxNumberOfProducts; } //todo: Get the cart from session?? var cartViewModel = WebSession.Current.Get <ShoppingCartViewModel>(WebSession.ShoppingCartSlot); var cart = cartViewModel == null?_cartApi.GetCartAsync().Result : cartViewModel.Cart; if (cart.LineItems.LineItem != null) { foreach (var li in cart.LineItems.LineItem) { if (li.Product != null) { vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, li.Product, _linkGenerator.GenerateShoppingCartLink()).Result; } if (vm != null) { break; } } } // If nothing is in the cart, or if no Offers were returned for this product, try to use the default offers. if (vm == null || vm.Offers == null || vm.Offers.Count == 0) { vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, _linkGenerator.GenerateShoppingCartLink()).Result; } // Set the empty cart promotion Id, which is used by an Ajax call to the ShoppingCartPart.GetUpdatedCrossSell() if (vm != null) { vm.EmptyCartPromotionId = emptyCartPromotionId; } OffersViewModelBuilder.RemoveCartItemsFromOffers(vm, cart); } else if (CurrentPage is ProductPage || CurrentPage is ShoppingCartInterstitialPage) { ProductDetailPageViewModel product; if (WebSession.Current.TryGet(WebSession.CurrentProductSlot, out product)) { if (product != null) { var crossSellProduct = product.Product; var scip = CurrentPage as ShoppingCartInterstitialPage; if (scip != null && scip.ProductID != null) { var scipPid = long.Parse(scip.ProductID); crossSellProduct = product.Product.Variations.Product.FirstOrDefault(p => p.Id == scipPid); } vm = _offersViewModelBuilder.GetCrossSellViewModelAsync(promotionId, crossSellProduct, _linkGenerator.GenerateShoppingCartLink()).Result; maxNumberOfProducts = 999; } } } if (vm == null) { vm = new CrossSellViewModel { Offers = new List <CrossSellOfferViewModel>() } } ; vm.Name = CurrentItem.Name; vm.Title = CurrentItem.Title; vm.PromotionId = promotionId; vm.MaxNumberOfProducts = maxNumberOfProducts ?? 0; return(PartialView(vm)); } }