public ActionResult CartPartial() { ShopCartPL cart = new ShopCartPL(); int quantity = 0; decimal price = 0m; if (Session["Cart"] != null) { var list = (List <ShopCartPL>)Session["Cart"]; foreach (var item in list) { quantity += item.Quantity; price += item.Quantity * item.Price; } cart.Quantity = quantity; cart.Price = price; } else { cart.Quantity = 0; cart.Price = 0m; } return(PartialView("_CartPartial", cart)); }
public void RemoveProduct(int prodId) { List <ShopCartPL> carts = Session["cart"] as List <ShopCartPL>; ShopCartPL cart = carts.FirstOrDefault(x => x.ProductPL.Id == prodId); carts.Remove(cart); }
public JsonResult AddProductToShopCart(int prodId) { List <ShopCartPL> carts = Session["cart"] as List <ShopCartPL>; ShopCartPL cart = carts.FirstOrDefault(x => x.ProductPL.Id == prodId); cart.Quantity++; var result = new { qty = cart.Quantity, price = cart.Price }; return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult AddToCartPartial(int id) { List <ShopCartPL> cart = Session["cart"] as List <ShopCartPL> ?? new List <ShopCartPL>(); ShopCartPL cartPL = new ShopCartPL(); var productBL = _productService.GetById(id); var productPL = _mapper.Map <ProductPL>(productBL); var productInCart = cart.FirstOrDefault(x => x.ProductPL.Id == id); if (productInCart == null) { cart.Add(new ShopCartPL() { ProductPL = productPL, Quantity = 1, Price = productPL.Price, ImagePath = productPL.ImagePath }); } else { productInCart.Quantity++; } int amount = 0; decimal price = 0m; foreach (var item in cart) { amount += item.Quantity; price += item.Quantity * item.Price; } cartPL.Quantity = amount; cartPL.Price = price; Session["cart"] = cart; return(PartialView("_AddToCartPartial", cartPL)); }
public ActionResult RemoveProductFromShopCart(int prodId) { List <ShopCartPL> carts = Session["cart"] as List <ShopCartPL>; ShopCartPL cart = carts.FirstOrDefault(x => x.ProductPL.Id == prodId); if (cart.Quantity > 1) { cart.Quantity--; } else { cart.Quantity = 0; carts.Remove(cart); } var result = new { qty = cart.Quantity, price = cart.Price }; return(Json(result, JsonRequestBehavior.AllowGet)); }