Example #1
0
        /// <summary>
        /// Validates if the specified item can be added to the cart
        /// </summary>
        /// <param name="itemSKU">The SKU of the item to be added to the cart</param>
        /// <param name="quant">The desired quantity of the item to be added to the cart</param>
        /// <param name="cartID">The ID of the current cart</param>
        /// <returns>True if the item can be added to the cart</returns>
        public bool ItemCanBeAdded(string itemSKU, int quant, int cartID)
        {
            string[] s_lastInsertedItem     = lastInsertedItem.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] s_lastInsertedQuantity = lastInsertedQuant.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] t_originalCartDetails  = DBOps.GetUserCart(cartID);

            for (int i = 0; i < s_lastInsertedItem.Length; i++)
            {
                if (s_lastInsertedItem[i].Trim() == itemSKU.Trim())
                {
                    int t_quant = Convert.ToInt32(s_lastInsertedQuantity[i]);

                    /// The variables in the cart object get updated before
                    /// this method is called. That is why we check if the
                    /// current quantity will equate to an amount that is
                    /// greater than 99 or any defined limit.
                    if (t_quant > 99 || DBOps.GetProductQuantity(itemSKU) <= 0)
                    {
                        lastInsertedPrice = t_originalCartDetails[1];
                        lastInsertedQuant = t_originalCartDetails[2];
                        totalItemQuantity = Convert.ToInt32(t_originalCartDetails[3]);
                        totalCartPrice    = Convert.ToDecimal(t_originalCartDetails[4]);
                        return(false);
                    }
                }
            }

            return(true);
        }
Example #2
0
        protected void lvw_items_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            string[] productDetails = ((String)e.CommandArgument).Split(',');

            try
            {
                _cart.RemoveItem(productDetails[0].Trim(), Convert.ToDecimal(productDetails[1].Trim()), Convert.ToInt32(productDetails[2].Trim()));
                cartDatasource.Update();
                _itemSKU   = productDetails[0].Trim();
                _itemQuant = DBOps.GetProductQuantity(_itemSKU) + Convert.ToInt32(productDetails[2].Trim());
                ProductsDataSource.Update();
            }
            catch (Exception)
            {
                // ignored
            }

            Button button = (Button)UpdatePanel1.FindControl("btn_checkout");

            if (DBOps.BuildUserCart(_userCartId).Rows.Count < 1)
            {
                if (_user != "-")
                {
                    //cartDatasource.Delete();
                    //DataOps.reassignUserCart(user);
                }
                button.Enabled  = false;
                button.CssClass = "btn btn-outline-secondary btn-block";
            }

            DataPager dp = (DataPager)lvw_items.FindControl("DataPager1");

            if (lvw_items.Items.Count <= 1)
            {
                dp.SetPageProperties(0, dp.MaximumRows, false);
            }

            lvw_items.DataSource  = DBOps.BuildUserCart(_userCartId);
            lvw_totals.DataSource = DBOps.BuildUserCartTotals(_userCartId);
            lvw_items.DataBind();
            lvw_totals.DataBind();

            SiteMaster master = Page.Master as SiteMaster;

            master.UpdateTotalCounters();
        }
        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 #4
0
        protected void tbx_qty_TextChanged(object sender, EventArgs e)
        {
            TextBox          textBox1 = (TextBox)sender;
            ListViewDataItem item     = (ListViewDataItem)textBox1.NamingContainer;
            TextBox          tb       = (TextBox)item.FindControl("tbx_qty"); //get the textbox in the proper listview item

            if (Convert.ToInt32(tb.Text) <= 0 || tb.Text == string.Empty || string.IsNullOrEmpty(tb.Text) || string.IsNullOrWhiteSpace(tb.Text) || (tb.Text == DBNull.Value.ToString(CultureInfo.InvariantCulture)))
            {
                tb.Text = "1";
            }
            else if (Convert.ToInt32(tb.Text) > 99)
            {
                tb.Text = "99";
            }

            Label lblSku   = (Label)item.FindControl("lbl_sku");
            Label lblPrice = (Label)item.FindControl("lbl_price");

            //int t_itemStock = DBOps.GetProductQuantity(lblSku.Text);
            int t_itemStock        = Convert.ToInt32(Session[lblSku.Text]);
            int t_cartQuantity     = Convert.ToInt32(tb.Text);
            int t_currCartQuantity = DBOps.GetItemQuantity(_userCartId, lblSku.Text);

            #region Old validation code
            //int t_sessionQuant = 0;

            //if (Session[lblSku.Text] != null)
            //{
            //    t_sessionQuant = Convert.ToInt32(Session[lblSku.Text]);
            //}
            ////else
            ////{
            ////    Session[lblSku.Text] = t_currCartQuantity;
            ////}
            #endregion


            try
            {
                _itemSKU = lblSku.Text;

                /// user adds a specified amount of the item to the cart
                if (t_currCartQuantity - t_cartQuantity < 0)
                {
                    if (t_itemStock >= t_cartQuantity /* || t_sessionQuant >= t_cartQuantity*/)
                    {
                        _cart.UpdateItem(lblSku.Text, Decimal.Parse(lblPrice.Text, NumberStyles.Currency), Convert.ToInt32(tb.Text));
                        cartDatasource.Update();

                        _itemQuant = DBOps.GetProductQuantity(_itemSKU) - Math.Abs(t_currCartQuantity - t_cartQuantity);
                        ProductsDataSource.Update();
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "notif",
                                                            string.Format("alert('ITEM NOT ADDED. The specified quantity of {0} is more than the available stock of the item.')",
                                                                          t_cartQuantity), true);
                    }
                }
                else
                {
                    _cart.UpdateItem(lblSku.Text, Decimal.Parse(lblPrice.Text, NumberStyles.Currency), Convert.ToInt32(tb.Text));
                    cartDatasource.Update();
                    _itemQuant = DBOps.GetProductQuantity(_itemSKU) + Math.Abs(t_currCartQuantity - t_cartQuantity);
                    ProductsDataSource.Update();
                }
            }
            catch (Exception)
            {
                // ignored
            }

            lvw_items.DataSource  = DBOps.BuildUserCart(_userCartId);
            lvw_totals.DataSource = DBOps.BuildUserCartTotals(_userCartId);
            lvw_items.DataBind();
            lvw_totals.DataBind();

            SiteMaster master = Page.Master as SiteMaster;
            master.UpdateTotalCounters();
        }
Example #5
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();
        }