// [ValidateAntiForgeryToken] public ActionResult AddProduct(AddProductModel addProduct) { if (ModelState.IsValid) { try { int quantity; if (String.IsNullOrEmpty(addProduct.Quantity) || !int.TryParse(addProduct.Quantity, out quantity)) { quantity = 1; } int offerId; if (String.IsNullOrEmpty(addProduct.OfferId) || !int.TryParse(addProduct.OfferId, out offerId)) { offerId = 0; } var cart = _cartApi.AddProductToCartAsync(Convert.ToInt32(addProduct.ProductId), quantity, offerId).Result; WebSession.Current.SetPersistentProperty(WebSession.ShoppingCartCount, cart.TotalItemsInCart.ToString(CultureInfo.InvariantCulture)); 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)); }
// virtual for testing only! protected virtual ActionResult InternalAddToCart(AddProductModel[] addProductModels, bool skipInterstitial = false, int cpeCode = 0, int scsCode = 0) { if (addProductModels != null) { var errorMessages = new List <string>(); // put the product(s) in the cart foreach (var addProductModel in addProductModels) { try { var cart = CartApi.AddProductToCartAsync(Convert.ToInt64(addProductModel.ProductId), Convert.ToInt32(addProductModel.Quantity), Convert.ToInt64(addProductModel.OfferId)).Result; WebSession.Current.SetPersistentProperty(WebSession.ShoppingCartCount, cart.TotalItemsInCart.ToString(CultureInfo.InvariantCulture)); } catch (Exception exc) { var tempErrorMessages = new List <string>(); var shopperApiException = exc as ShopperApiException; if (shopperApiException != null) { switch (shopperApiException.ShopperApiError.Code) { case "inventory-unavailable-error": tempErrorMessages.Add(Res.ShoppingCart_AddProductOutOfStock); break; default: tempErrorMessages.Add(Res.ShoppingCart_AddProductFailed); break; } } else { tempErrorMessages.Add(Res.ShoppingCart_AddProductFailed); } errorMessages.AddRange(tempErrorMessages); } } if (errorMessages.Any()) { var model = ShoppingCartViewModelBuilder.GetShoppingCartViewModelAsync().Result; model.ErrorMessages = errorMessages.ToArray(); WebSessionSet(WebSession.ShoppingCartSlot, model); return(Index()); } if (addProductModels.Length == 1 && !skipInterstitial) { var productId = int.Parse(addProductModels[0].ProductId); var interstitialLink = LinkGenerator.GenerateInterstitialLink(productId); if (!String.IsNullOrEmpty(interstitialLink)) { return(Redirect(interstitialLink)); } var siteRoot = CmsFinder.FindSitePageFromSiteId(WebSession.Current.SiteId); if (siteRoot != null && !String.IsNullOrEmpty(siteRoot.PromotionId)) { var offers = OffersViewModelBuilder.GetCrossSellViewModelAsync(siteRoot.PromotionId, CatalogApi.GetProductUri(productId), LinkGenerator.GenerateShoppingCartLink()).Result; if (offers != null && offers.Offers.Count > 0) { interstitialLink = LinkGenerator.GenerateInterstitialLink(); if (!String.IsNullOrEmpty(interstitialLink)) { return(Redirect(interstitialLink + "/" + productId)); } } } } } var cartLink = LinkGenerator.GenerateShoppingCartLink(); if (scsCode != 0) { cartLink += (cartLink.LastIndexOf('?') == -1 ? "?" : "&") + ParamScsErrorCode + "=" + scsCode; } return(Redirect(cartLink)); }