private void updAvailListInOptimisticScenario(DropDownList ddl, int prodAttrId, int alreadyInCart, int newQuantity, LinkButton lnk) { List <int> qtyList; PRODUCT_ATTRIBUTE prodAttr = ApplicationContext.Current.Products.GetProductAvailability(prodAttrId, out qtyList, alreadyInCart); if (!qtyList.Contains(newQuantity)) { ddl.SelectedValue = alreadyInCart.ToString(); lnk.Text = alreadyInCart.ToString(); lblMessage.Text = Resources.Lang.InsufficientAvailabilityMessage; CartSession.Find(c => c.ProductAttributeId == prodAttrId).Quantity = alreadyInCart; } }
protected void ddlQty_SelectedIndexChanged(object sender, EventArgs e) { DropDownList ddl = sender as DropDownList; int selectedQty; if (Int32.TryParse(ddl.SelectedValue, out selectedQty)) { RepeaterItem item = (RepeaterItem)ddl.NamingContainer; HiddenField field = (HiddenField)item.FindControl("ProdAttrID"); int prodAttrId; if (Int32.TryParse(field.Value, out prodAttrId)) { SessionCart cart = CartSession.Find(c => c.ProductAttributeId == prodAttrId); if (cart != null) { // we already have the right version in the session variable cartsession int oldQuantity = cart.Quantity; SHOPPING_CART shopping = new SHOPPING_CART() { ID = cart.Id, ProdAttrID = prodAttrId }; // session var cart.Quantity = selectedQty; cart.DateAdded = DateTime.Now; shopping.Quantity = selectedQty; shopping.DateAdded = DateTime.Now; shopping.ProductAttributeVersion = cart.ProductAttributeVersion; LinkButton lnk = (LinkButton)item.FindControl("lnkEdit"); try { ApplicationContext.Current.Carts.Update(shopping, oldQuantity); lblMessage.Text = String.Empty; } catch (Exception ex) { BasePage.Log(ex, ex.Message, ex.StackTrace, "Cart Control"); updAvailListInOptimisticScenario(ddl, prodAttrId, oldQuantity, selectedQty, lnk); } finally { DataBind(); ddl.Visible = false; lnk.Visible = true; } } } } }
protected void rptDetails_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandSource is LinkButton) { LinkButton lnk = e.CommandSource as LinkButton; if (!String.IsNullOrWhiteSpace(lnk.CommandArgument)) { int prodAttrID; if (Int32.TryParse(lnk.CommandArgument, out prodAttrID)) { if (lnk.ID == "lnkEdit") { RepeaterItem item = (RepeaterItem)lnk.NamingContainer; DropDownList ddlQty = (DropDownList)item.FindControl("ddlQty"); ddlQty.Visible = true; lnk.Visible = false; List <int> qtyList; PRODUCT_ATTRIBUTE prodAttr = ApplicationContext.Current.Products.GetProductAvailability(prodAttrID, out qtyList, Int32.Parse(lnk.Text)); // updating version SessionCart cart = CartSession.Find(c => c.ProductAttributeId == prodAttrID); cart.ProductAttributeVersion = prodAttr.Version; ddlQty.DataSource = qtyList; ddlQty.DataBind(); ddlQty.SelectedValue = lnk.Text; } else if (lnk.ID == "lnkRemove") { ApplicationContext.Current.Carts.DeleteById(CartSession.First().Id, prodAttrID); DataBind(); } if (CartSession == null || CartSession.Count == 0) { lblMessage.Text = Resources.Lang.EmptyCartLabel; rptDetails.Visible = false; } else { lblMessage.Text = String.Empty; } } } } }
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/"); }