예제 #1
0
        private CartProductViewModel AdditionChecks(CartProductViewModel product)
        {
            IEnumerable <AdminProductViewModel> products = new StockFunctions(_constring, null, stockFunctions).RunGetStock();

            foreach (var p in products)
            {
                foreach (var s in p.Stock)
                {
                    if (s.Id == product.StockId && product.Quantity > s.Quantity)
                    {
                        product.Quantity = s.Quantity;
                    }
                }
            }
            if (product.Quantity < 1)
            {
                product.Quantity = 1;
            }
            return(product);
        }
예제 #2
0
        public void AddToShoppingCart(CartProductViewModel product)
        {
            product = AdditionChecks(product);

            var cartlist     = new List <CartProductViewModel>();
            var stringObject = _session.GetString("shoppingCart");

            if (!string.IsNullOrEmpty(stringObject))
            {
                cartlist = JsonConvert.DeserializeObject <List <CartProductViewModel> >(stringObject);
            }

            if (cartlist.Any(a => a.StockId == product.StockId))
            {
                cartlist.Find(a => a.StockId == product.StockId).Quantity += product.Quantity;
            }
            else
            {
                cartlist.Add(product);
            }

            stringObject = JsonConvert.SerializeObject(cartlist);
            _session.SetString("shoppingCart", stringObject);
        }