public async Task <IActionResult> Index() { string tempCartId = httpContextAccessor.HttpContext.Request.Cookies["tempCartId"]; List <TempCartItem> tempCarts = cartServices.GetTempCartList(tempCartId); if (tempCarts != null) { foreach (TempCartItem item in tempCarts) { cartServices.addToCart(UserName, item.Product.Id, item.Quantity); } await cartServices.DeleteTempCart(tempCartId); } List <CartItem> cart = cartServices.GetCartList(UserName); CartListViewModel viewModel = new CartListViewModel(); if (cart != null) { viewModel.CartItems = cart .Select(x => new CartViewModel() { Id = x.Id, ImageAddress = x.Product.PictureAddress, Price = x.Product.Price, Quantity = x.Quantity, Title = x.Product.Title }).ToList(); viewModel.TotalPrice = cart.Sum(x => x.Quantity * x.Product.Price); } else { viewModel = new CartListViewModel() { CartItems = new List <CartViewModel>(), TotalPrice = 0 }; } return(View(viewModel)); }
public IActionResult GetProductOfOpenCart() { CartListViewModel result = new CartListViewModel(); if (UserName == null || UserName == "") { string tempCartId = httpContextAccessor.HttpContext.Request.Cookies["tempCartId"]; var cart = cartServices.GetTempCartList(tempCartId); if (cart != null) { result.CartItems = cart.Where(c => !c.DeletedDate.HasValue && !c.Product.DisableDate.HasValue && !c.Product.RemoveDate.HasValue) .Select(x => new CartViewModel() { Id = x.Id, ImageAddress = x.Product.PictureAddress, Price = x.Product.Price, Quantity = x.Quantity, Title = x.Product.Title }).ToList(); result.TotalPrice = result.CartItems.Sum(x => x.Price * x.Quantity); } else { result = new CartListViewModel() { CartItems = new List <CartViewModel>(), TotalPrice = 0 }; } } else { var cart = cartServices.GetCartList(UserName); if (cart != null) { result.CartItems = cart.Where(c => !c.DeletedDate.HasValue && !c.RemoveDate.HasValue && !c.Product.DisableDate.HasValue && !c.Product.RemoveDate.HasValue) .Select(x => new CartViewModel() { Id = x.Id, ImageAddress = x.Product.PictureAddress, Price = x.Product.Price, Quantity = x.Quantity, Title = x.Product.Title }).ToList(); result.TotalPrice = result.CartItems.Sum(x => x.Price * x.Quantity); } else { result = new CartListViewModel() { CartItems = new List <CartViewModel>(), TotalPrice = 0 }; } } return(Json(result)); }