コード例 #1
0
        public async Task <IActionResult> AddToCart(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Product product = await _db.Products.FindAsync(id);

            if (product == null)
            {
                return(NotFound());
            }

            List <ProductCartVM> products;
            string existingCart = Request.Cookies["cart"];

            if (existingCart == null)
            {
                products = new List <ProductCartVM>();
            }
            else
            {
                products = JsonConvert.DeserializeObject <List <ProductCartVM> >(existingCart);
            }

            ProductCartVM existingProduct = products.FirstOrDefault(p => p.Id == id);

            if (existingProduct == null)
            {
                ProductCartVM newPro = new ProductCartVM
                {
                    Id       = product.Id,
                    Image    = product.Image,
                    Name     = product.Name,
                    Price    = product.Price,
                    Quantity = 1
                };
                products.Add(newPro);
            }
            else
            {
                existingProduct.Quantity++;
            }

            string cart = JsonConvert.SerializeObject(products);

            Response.Cookies.Append("cart", cart, new CookieOptions {
                MaxAge = TimeSpan.FromDays(14)
            });
            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        public ActionResult CheckOut()
        {
            int _userId = (from s in db.Sessions select s.Id).FirstOrDefault();

            if (_userId != 0)
            {
                double totalPrice    = 0.0;
                int    totalQuantity = 0;

                foreach (var c in cart)
                {
                    totalQuantity += (int)c.prodQuan;
                    totalPrice    += (double)c.prodPrice * (int)c.prodQuan;
                    var prodChange = (from x in db.Products where x.prodId == c.prodId select x).FirstOrDefault();
                    if (prodChange != null)
                    {
                        prodChange.prodQuan -= (int)c.prodQuan;
                    }
                }

                var query = (from x in db.Carts where x.Id == _userId select x);
                foreach (var _c in query)
                {
                    _c.totalPrice    = totalPrice;
                    _c.totalQuantity = totalQuantity;
                }
                var pcs = new List <ProductCart>();
                foreach (var c in cart)
                {
                    ProductCart pc = new ProductCart()
                    {
                        productId = c.prodId,
                        cartId    = _userId,
                        // Product = c,
                        checkoutDate      = System.DateTime.Now,
                        quantityPurchased = c.prodQuan
                    };
                    pcs.Add(pc);
                }
                db.ProductCarts.AddRange(pcs);
                db.SaveChanges();

                User _user = (from u in db.Users where u.userId == _userId select u).FirstOrDefault();
                //Buat ViewModels
                pcVM = new ProductCartVM()
                {
                    User          = _user,
                    Products      = cart.ToList(),
                    totalPrice    = totalPrice,
                    totalQuantity = totalQuantity
                };


                //clear cart after checkout success
                cart.Clear();
                return(View("Invoice", pcVM));
            }
            else
            {
                return(RedirectToAction("AddToCart"));
            }
        }