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();
        }
Example #2
0
        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();
        }