/// <summary> /// Constructor called to create the purchased product off of the shopping cart product /// </summary> /// <param name="shopCartProd"></param> public PurchasedProduct(string shippingMethodName, ShoppingCartProduct shopCartProd) : this() { Id = shopCartProd.Id; Name = shopCartProd.Name; MainImagePath = shopCartProd.MainImagePath; Quantity = shopCartProd.Quantity; PurchasePrice = shopCartProd.GetRealItemPrice(); ShippingPrice = shopCartProd.GetRealShippingCost(shippingMethodName); SelectedCheckoutProperties = shopCartProd.SelectedCheckoutProperties; }
/// <summary> /// Called whenever the user wishes to add a new item to their shopping cart. /// </summary> /// <returns></returns> public ActionResult AddItemToCart(string id) { try { SiteContext.RecordPageView("Ecommerce_ViewShoppingCart"); Product Prod = ProductDAO.LoadByBsonId(id); if (Prod != null && !Prod.Id.Equals(string.Empty)) { ShoppingCartProduct ShopCartProd = new ShoppingCartProduct(Prod); //first build the key/value pairs that the user selected in the view product form List<CheckoutPropertySettingKey> CheckoutPropertySettingKeys = new List<CheckoutPropertySettingKey>(); if (Prod.CheckoutPropertyList != null && Prod.CheckoutPropertyList.Count > 0) { foreach (var CheckProp in Prod.CheckoutPropertyList) { if (!string.IsNullOrWhiteSpace(Request["checkProd_" + CheckProp.Name])) { CheckoutPropertySettingKeys.Add(new CheckoutPropertySettingKey(CheckProp.Name, Request["checkProd_" + CheckProp.Name])); } } } //if there are even custom settings if (Prod.CheckoutPropertySettingsList != null && Prod.CheckoutPropertySettingsList.Count > 0) { //now find the custom setting with the customers selected key/values foreach (var CheckPropSetting in Prod.CheckoutPropertySettingsList) { int NumberOfMatches = 0; if (CheckPropSetting.CheckoutPropertySettingKeys != null && CheckPropSetting.CheckoutPropertySettingKeys.Count > 0) { foreach (var CheckPropSettingKey in CheckPropSetting.CheckoutPropertySettingKeys) { CheckoutPropertySettingKey MatchedCheckPropSetKey = CheckoutPropertySettingKeys.Where(e => e.Key.Equals(CheckPropSettingKey.Key) && e.Value.Equals(CheckPropSettingKey.Value)).FirstOrDefault(); if (MatchedCheckPropSetKey != null && !string.IsNullOrWhiteSpace(MatchedCheckPropSetKey.Key) && !string.IsNullOrWhiteSpace(MatchedCheckPropSetKey.Value)) { NumberOfMatches++; } } } //if this checkout property setting matched the same key/value pairs the user selected if (NumberOfMatches > 0 && NumberOfMatches == CheckoutPropertySettingKeys.Count) { ShopCartProd.CheckoutPropertySetting = CheckPropSetting; break; } } } ShopCartProd.SelectedCheckoutProperties = CheckoutPropertySettingKeys; ShoppingCartProduct AlreadyExistsShopCartProd = SiteContext.ShoppingCartProductList.Where(e => e.CartUniqueId.Equals(ShopCartProd.CartUniqueId)).FirstOrDefault(); //if not null then this user already has this exact prod/setting match, just increase quantity if (AlreadyExistsShopCartProd != null) { if (AlreadyExistsShopCartProd.CanAddToCartOrIncreaseQuantity(AlreadyExistsShopCartProd.Quantity + 1)) { AlreadyExistsShopCartProd.Quantity++; SiteContext.RemoveShoppingCartProduct(AlreadyExistsShopCartProd.CartUniqueId); SiteContext.AddShoppingCartProduct(AlreadyExistsShopCartProd); } else { //add user message AddWebUserMessageToSession(Request, AlreadyExistsShopCartProd.CreateOutOfStockMessage(), NEUTRAL_MESSAGE_TYPE); } } //else add a brand new product to the shopping cart else { if (ShopCartProd.CanAddToCartOrIncreaseQuantity(1)) { SiteContext.AddShoppingCartProduct(ShopCartProd); } else { //add user message AddWebUserMessageToSession(Request, ShopCartProd.CreateOutOfStockMessage(), NEUTRAL_MESSAGE_TYPE); } } ViewBag.ShoppingCartModel = GetModel(); return View("ViewCart", String.Format("~/Templates/{0}/Views/Shared/Template.Master", Models.ChimeraTemplate.TemplateName)); } } catch (Exception e) { CompanyCommons.Logging.WriteLog("ChimeraWebsite.Controllers.ShoppingCartController.AddItemToCart() " + e.Message); } //TODO: return 404 page instead? return RedirectToAction("Index", "Home"); }
/// <summary> /// Add a new shopping cart product /// </summary> /// <param name="shoppingCartProduct"></param> public static void AddShoppingCartProduct(ShoppingCartProduct shoppingCartProduct) { List<ShoppingCartProduct> NewShoppingCartProductList = ShoppingCartProductList; NewShoppingCartProductList.Add(shoppingCartProduct); ShoppingCartProductList = NewShoppingCartProductList; }