public void SaveToCookie(ProductViewModel product, int quantity) { if (CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey) == null) { List <CartItemViewModel> cart = new List <CartItemViewModel>(); cart.Add(new CartItemViewModel { Product = product, Quantity = 1 }); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } else { List <CartItemViewModel> cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); int index = cart.FindIndex(x => x.Product.Id == product.Id); if (index != -1) { cart[index].Quantity += quantity; } else { cart.Add(new CartItemViewModel { Product = product, Quantity = quantity }); } CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } }
public void GetCart() { List <CartItemViewModel> cart = new List <CartItemViewModel>(); var CartSessionKey = _configuration.GetSection("CartSessionKey").Value; cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); ViewBag.cart = cart; ViewBag.total = (cart != null) ? cart.Sum(item => item.Product.Price * item.Quantity) : 0; ViewBag.NumItem = (cart != null)?cart.Sum(x => x.Quantity):0; }
public void DeleteItemFromCookie(int productId) { List <CartItemViewModel> cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); int index = cart.FindIndex(x => x.Product.Id == productId); if (index != -1) { cart.RemoveAt(index); CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } }
public void UpdateToCookie(int productId, int quantity) { List <CartItemViewModel> cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); int index = cart.FindIndex(x => x.Product.Id == productId); if (index != -1) { cart[index].Quantity = quantity; CookieHelpers.SetObjectAsJson(HttpContext.Response.Cookies, CartSessionKey, cart, null); } }
public IActionResult Index() { var x = GetCartAsync(); List <CartItemViewModel> cart = new List <CartItemViewModel>(); cart = CookieHelpers.GetObjectFromJson <List <CartItemViewModel> >(HttpContext.Request.Cookies, CartSessionKey); ViewBag.cart = cart; ViewBag.count = cart.Count(); ViewBag.total = (cart != null) ? cart.Sum(item => item.Product.Price * item.Quantity) : 0; if (section != null) { ViewBag.IsLogged = true; } return(View()); }
public override void OnActionExecuting(ActionExecutingContext context) { section = CookieHelpers.GetObjectFromJson <string>(Request.Cookies, "Token"); GetCart(); if (section != null) { var userPrincipal = ValidateToken(section); ViewBag.ImagePath = userPrincipal.Claims.FirstOrDefault(c => c.Type == "Picture").Value; ViewBag.UserName = userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value; ViewBag.Email = userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value; ViewBag.UserId = userPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; ViewBag.CartId = userPrincipal.Claims.FirstOrDefault(c => c.Type == "CartId").Value; } languageDefauleId = _configuration.GetSection("LanguageDefaultId").Value; base.OnActionExecuting(context); }