Example #1
0
        public IActionResult AddtoCart(int quantity, int ProductId)
        {
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;

            var cartUsername = GetCartUserName();

            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = quantity,
                    Price     = price,
                    Username  = cartUsername
                };
                _context.Cart.Add(cart);
            }
            else
            {
                cartItem.Quantity += quantity;
                _context.Update(cartItem);
            }



            _context.SaveChanges();
            return(RedirectToAction("Cart"));
        }
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            //identify product Price
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;
            //Determine Username
            var cartUsername = GetCartUserName();

            //Check if this user's products already exists in the cart. if so, update the quantity

            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                //create and save a new cart object
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = Quantity,
                    Price     = price,
                    Username  = cartUsername
                };
                _context.Cart.Add(cart);
            }
            else
            {
                cartItem.Quantity += Quantity;
                _context.Update(cartItem);
            }

            _context.SaveChanges();
            //show the cart page
            return(RedirectToAction("Cart"));
        }
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            //Identify product price
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;

            //Create and save new cart object
            var cart = new Cart
            {
                ProductId = ProductId,
                Quantity  = Quantity,
                Price     = price,
                Username  = "******"
            };

            _context.Cart.Add(cart);
            _context.SaveChanges();

            return(RedirectToAction("Cart"));
        }
Example #4
0
        public IActionResult AddToCart(int Quantity, int ProductId)
        {
            //Identify product Price
            var product = _context.Product.SingleOrDefault(p => p.ProductId == ProductId);
            var price   = product.Price;
            // Determine Username, since it is currently hard coded

            var cartUsername = GetCartUserName();

            // Check if THIS USER's product already exists in the cart. If so, update the quantity

            var cartItem = _context.Cart.SingleOrDefault(c => c.ProductId == ProductId && c.Username == cartUsername);

            if (cartItem == null)
            {
                var cart = new Cart
                {
                    ProductId = ProductId,
                    Quantity  = Quantity,
                    Price     = price,
                    Username  = cartUsername
                };
                // Create and save a new Cart Object
                _context.Cart.Add(cart);
            }

            else
            {
                cartItem.Quantity += Quantity; // Add the new quantity to the existing quantity
                _context.Update(cartItem);
            }

            _context.SaveChanges();

            return(RedirectToAction("Cart"));
        }