コード例 #1
0
        public IActionResult OnPostPlus(int id)
        {
            CartItems = HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList();

            var productiInCart = CartItems.SingleOrDefault(x => x.Product.Id == id);
            var cartItemPlus   = cartBL.PlusCartItem(productiInCart);

            CartItems
            .Where(x => x.Id == id)
            .Select(x => { x.Quantity = cartItemPlus.Quantity; return(x); }).ToList();
            if (cartItemPlus.Quantity == productiInCart.Product.Quantity)
            {
                TempData["Message"] = $"You have reached the limit for {productiInCart.Product.Name}!";
            }
            HttpContext.Session.SetObjectAsJson("CartItems", CartItems);
            CartItems = HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList();
            return(RedirectToPage("./ShoppingCart"));
        }
コード例 #2
0
ファイル: CartController.cs プロジェクト: jawadAhmed264/RDTH
        public IActionResult AddToCart(int Id, int qty)
        {
            List <CartItem> CartItems;
            var             CartSession = HttpContext.Session.GetString("Cart");
            Cart            Cart;

            if (CartSession == null)
            {
                Cart      = new Cart();
                CartItems = new List <CartItem>();
            }
            else
            {
                Cart      = JsonConvert.DeserializeObject <Cart>(HttpContext.Session.GetString("Cart"));
                CartItems = Cart.ItemList;
            }

            SetBox sb = _setBoxService.GetById(Id);

            if (CartItems.Any(i => i.Product.Name == sb.Name))
            {
                CartItems.SingleOrDefault(i => i.Id == Id).Qty   += qty;
                CartItems.SingleOrDefault(i => i.Id == Id).Price += (qty * sb.Price);
            }
            else
            {
                CartItem item = new CartItem
                {
                    Id      = ++CartItemId,
                    Product = sb,
                    Qty     = qty,
                    Price   = qty * sb.Price
                };

                CartItems.Add(item);
            }
            Cart.ItemList   = CartItems;
            Cart.TotalPrice = Cart.ItemList.Sum(m => m.Price);
            Cart.TotalItems = Cart.ItemList.Sum(m => m.Qty);

            HttpContext.Session.SetString("Cart", JsonConvert.SerializeObject(Cart));
            return(RedirectToAction("Index"));
        }
コード例 #3
0
        public IActionResult OnPostMinus(int id)
        {
            CartItems = HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList();
            var productiInCart = CartItems.SingleOrDefault(x => x.Product.Id == id);
            var cartItemMinus  = cartBL.MinusCartItem(productiInCart);

            if (cartItemMinus == null)
            {
                CartItems.Remove(productiInCart);
            }
            else
            {
                CartItems
                .Where(x => x.Id == id)
                .Select(x => { x.Quantity = cartItemMinus.Quantity; return(x); }).ToList();
            }
            HttpContext.Session.SetObjectAsJson("CartItems", CartItems);
            CartItems = HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList();
            return(RedirectToPage("./ShoppingCart"));
        }
コード例 #4
0
        public IActionResult OnGetBuy(int id)
        {
            var product = productData.GetProductById(id);

            if (HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems") != null)
            {
                CartItems = HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList();
                var productiInCart = CartItems.SingleOrDefault(x => x.Product.Id == id);

                if (CartItems.Contains(productiInCart))
                {
                    return(RedirectToPage("./ShoppingCart"));
                }
            }
            CartItems.Add(cartBL.AddCartItem(product));

            HttpContext.Session.SetObjectAsJson("CartItems", CartItems);
            CartItems = HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList();
            return(RedirectToPage("./ShoppingCart"));
            //return Page();
        }