protected void lnkAddToBasket_Click(object sender, EventArgs e) { string CartID = String.Empty; Guid g = Guid.NewGuid(); UniqueIdGenerator unique = UniqueIdGenerator.GetInstance(); string cartId = unique.GetBase32UniqueId(g.ToByteArray(), 20).ToLower(); if (CartSession == null || CartSession.Count == 0) { CartID = cartId; CartSession = new List <SessionCart>(); } else { List <SessionCart> cSession = CartSession.OrderByDescending(c => c.DateAdded).ToList(); SessionCart sessionCart = cSession.First(); if (sessionCart.DateAdded.AddMinutes(Configuration.CartExpirationValue) < DateTime.Now) { RefreshCart(); CartID = cartId; } else { CartID = CartSession.First().Id; } } SHOPPING_CART cart = new SHOPPING_CART(); cart.ID = CartID; cart.FrontEnd = true; cart.CampaignID = CampaignID; cart.CustomerID = CurrentCustomer.Id; cart.DateAdded = DateTime.Now; cart.ProductID = ProductID; int num = 0; if (!Int32.TryParse(ddlSize.SelectedValue, out num)) { return; } cart.ProdAttrID = num; num = 0; if (!Int32.TryParse(ddlQuantity.SelectedValue, out num) || num == 0) { return; } cart.Quantity = num; // the versions list of lists is created each time the product popup is shown, and destroyed each time it is closed if (Version != null) { cart.ProductAttributeVersion = Version; } else { throw new ApplicationException("Session is compromised! Cannot proceed."); } SessionCart sC; try { // already in the cart if (CartSession != null && CartSession.Count > 0 && (sC = CartSession.Find(c => c.ProductAttributeId == cart.ProdAttrID)) != null) { // sum with old quantity cart.Quantity += sC.Quantity; ApplicationContext.Current.Carts.Update(cart, sC.Quantity); // updating session with last quantity and last prod-attr version sC.Quantity = cart.Quantity; sC.ProductAttributeVersion = cart.ProductAttributeVersion; } else { ApplicationContext.Current.Carts.Insert(cart); sC = new SessionCart(cart); CartSession.Add(sC); } TotalAmount = ApplicationContext.Current.Carts.GetShoppingCartTotalAmount(CartID); } catch (Exception ex) { //TODO log error Log(ex, ex.Message, ex.StackTrace, "Product.AddToCart"); List <int> qtyList; PRODUCT_ATTRIBUTE prodAttr = ApplicationContext.Current.Products.GetProductAvailability(cart.ProdAttrID, out qtyList); Version = prodAttr.Version; ddlQuantity.DataSource = qtyList; ddlQuantity.DataBind(); if (!qtyList.Contains(cart.Quantity)) { lblMessage.Text = Resources.Lang.InsufficientAvailabilityMessage; } if (qtyList.Count == 0) { ddlQuantity.Enabled = false; lnkAddToBasket.Enabled = false; loadProductAttributes(); } //refreshing the size ddl loadProductAttributes(); updPanelDDL.Update(); return; } Version = null; Response.Redirect("/cart/mycart/"); }
/// <summary> /// Adds a new product to this session cart /// </summary> /// <param name="CustomerID"></param> /// <param name="CampaignID"></param> /// <param name="ProductId"></param> /// <param name="ProdAttrId"></param> /// <param name="Quantity"></param> public void AddProductToCart(int CustomerID, int CampaignID, int ProductId, int ProdAttrId, int Quantity, string BrandName) { if (CartID == null) { Guid g = Guid.NewGuid(); UniqueIdGenerator unique = UniqueIdGenerator.GetInstance(); string cartId = unique.GetBase32UniqueId(g.ToByteArray(), 20).ToLower(); CartID = cartId; } SHOPPING_CART cart = new SHOPPING_CART(); cart.ID = CartID; cart.FrontEnd = false; cart.CampaignID = CampaignID; cart.CustomerID = CustomerID; cart.DateAdded = DateTime.Now; cart.ProductID = ProductId; cart.ProdAttrID = ProdAttrId; cart.Quantity = Quantity; cart.BrandName = BrandName; // the versions list of lists is created each time the product popup is shown, and destroyed each time it is closed if (Versions != null) { cart.ProductAttributeVersion = Versions.Where(x => x.Key == ProductId).FirstOrDefault().Value.Where(y => y.Key == ProdAttrId).FirstOrDefault().Value; } else { throw new ApplicationException("Session is compromised! Cannot proceed."); } if (CartSession == null) { CartSession = new List <SHOPPING_CART>(); } List <SHOPPING_CART> carts = CartSession; SHOPPING_CART sc; // already in the cart if (carts.Count > 0 && (sc = carts.Where(c => c.ID == cart.ID && c.ProdAttrID == cart.ProdAttrID).FirstOrDefault()) != null) { cart.Quantity += sc.Quantity; ApplicationContext.Current.Carts.Update(cart, sc.Quantity); // updating session with last quantity and last prod-attr version sc.Quantity = cart.Quantity; sc.ProductAttributeVersion = cart.ProductAttributeVersion; //sc = cart; } else { ApplicationContext.Current.Carts.Insert(cart); // already has the last version set from the context saving CartSession.Add(cart); } // refreshing session, optional //ApplicationContext.Current.Carts.GetShoppingCartItems(CartID); DataBind(CartSession); }