/// <summary> /// Updates the details of the specified item in the cart /// </summary> /// <param name="itemSKU">The SKU of the item to be updated</param> /// <param name="price">The price of the item to be updated</param> /// <param name="quant">The quantity of the item to be updated</param> public void UpdateItem(string itemSKU, decimal price, int quant) { int index = -1; string[] holdData = DBOps.GetCartItems(cartID); lastInsertedItem = holdData[0]; lastInsertedPrice = holdData[1]; lastInsertedQuant = holdData[2]; totalCartPrice = Convert.ToDecimal(holdData[3]); totalItemQuantity = Convert.ToInt32(holdData[4]); if (lastInsertedItem != string.Empty && lastInsertedItem != null) { string[] items = lastInsertedItem.Split(','); List <string> newItems = new List <string>(); List <string> newQuants = new List <string>(); List <string> newPrices = new List <string>(); if (Regex.IsMatch(lastInsertedItem, string.Format(@"\b{0}\b", itemSKU))) { for (int i = 0; i < items.Length; i++) { if (items[i] == itemSKU) { index = i; break; } } if (index != -1) { string[] prices = lastInsertedPrice.Split(','); string[] quantities = lastInsertedQuant.Split(','); int originalQuant = Convert.ToInt32(quantities[index].Trim()); int qT = Convert.ToInt32(quant); quantities[index] = qT.ToString(); prices[index] = (price * qT).ToString(); lastInsertedPrice = string.Join(",", prices); lastInsertedQuant = string.Join(",", quantities); totalCartPrice = ComputeTotalPrice(); totalItemQuantity = ComputeTotalItems(); } } } }
protected void ProductList_ItemCommand(object sender, ListViewCommandEventArgs e) { if (_currUser != "-" && _tempId != 0) { _cart.cartID = _userCartId; _cart.cartOwner = _currUser; string[] items = DBOps.GetCartItems(_userCartId); _cart.lastInsertedItem = items[0]; _cart.lastInsertedPrice = items[1]; _cart.lastInsertedQuant = items[2]; _cart.totalItemQuantity = Convert.ToInt32(items[3]); _cart.totalCartPrice = Convert.ToDecimal(items[4]); } else if (_currUser != "-" && _tempId == 0) { _cart.cartID = _userCartId; userInfoDataSource.Update(); } else { _cart.cartID = _userCartId; } string[] productDetails = ((String)e.CommandArgument).Split(','); _cart.AddItem(productDetails[0].Trim(), Convert.ToDecimal(productDetails[1].Trim()), 1); _itemSku = productDetails[0].Trim(); bool CanBeAdded = _cart.ItemCanBeAdded(_itemSku, 1, _userCartId); int productQuant = DBOps.GetProductQuantity(_itemSku); if (Session[_itemSku] == null) { Session[_itemSku] = productQuant; } if (!DBOps.RecordExists(_userCartId)) { if (CanBeAdded) { CartDataSource.Insert(); _itemQuant = productQuant - 1; ProductsDataSource.Update(); } } else { if (CanBeAdded && productQuant > 0) { _itemQuant = productQuant - 1; CartDataSource.Update(); ProductsDataSource.Update(); } else if (productQuant == 0) { ScriptManager.RegisterStartupScript(this, GetType(), "notif", "alert('ITEM NOT ADDED. This product is currently out of stock. Try again later.')", true); } else if (!CanBeAdded) { ScriptManager.RegisterStartupScript(this, GetType(), "notif", $"alert('ITEM NOT ADDED. You either have the maximum number of it in your cart or adding the specified amount of {1} will exceed the limit of 99.')", true); } } SiteMaster master = Page.Master as SiteMaster; master.UpdateTotalCounters(); }
protected void btnAddToCart_Click(object sender, EventArgs e) { if (_currUser != "-" && _tempId != 0) { _cart.cartID = _userCartId; _cart.cartOwner = _currUser; string[] items = DBOps.GetCartItems(_userCartId); _cart.lastInsertedItem = items[0]; _cart.lastInsertedPrice = items[1]; _cart.lastInsertedQuant = items[2]; _cart.totalItemQuantity = Convert.ToInt32(items[3]); _cart.totalCartPrice = Convert.ToDecimal(items[4]); } else if (_currUser != "-" && _tempId == 0) { _cart.cartID = _userCartId; userInfoDataSource.Update(); } else { _cart.cartID = _userCartId; } _itemSku = _result?["sku"].ToString(); int productQuant = DBOps.GetProductQuantity(_itemSku); if (Session[_itemSku] == null) { Session[_itemSku] = productQuant; } int t_itemStock = DBOps.GetProductQuantity(_itemSku); int t_cartQuantity = Convert.ToInt32(tbxQty.Text); bool proceed = t_itemStock >= t_cartQuantity; _cart.AddItem(_result?["sku"].ToString(), Convert.ToDecimal($"{_result?["price"]}"), Convert.ToInt32(tbxQty.Text)); if (!DBOps.RecordExists(_userCartId)) { if (proceed) { CartDataSource.Insert(); _itemQuant = productQuant - Convert.ToInt32(tbxQty.Text); Products.Update(); } else if (productQuant == 0) { ScriptManager.RegisterStartupScript(this, GetType(), "notif", "alert('ITEM NOT ADDED. This product is currently out of stock. Try again later.')", true); } else { ScriptManager.RegisterStartupScript(this, GetType(), "notif", $"alert('ITEM NOT ADDED. You either have the maximum number of it in your cart or adding the specified amount of {tbxQty.Text} will exceed the limit of 99.')", true); } } else { if (proceed) { CartDataSource.Update(); _itemQuant = productQuant - Convert.ToInt32(tbxQty.Text); Products.Update(); } else if (productQuant == 0) { ScriptManager.RegisterStartupScript(this, GetType(), "notif", "alert('ITEM NOT ADDED. This product is currently out of stock. Try again later.')", true); } else { ScriptManager.RegisterStartupScript(this, GetType(), "notif", $"alert('ITEM NOT ADDED. You either have the maximum number of it in your cart or adding the specified amount of {tbxQty.Text} will exceed the limit of 99.')", true); } } string[] totals = DBOps.GetUserCartTotals(_cart.cartID); SiteMaster master = Page.Master as SiteMaster; master.UpdateTotalCounters(); }