public ActionResult CartList() { var customer = workContext.GetAuthenticatedCustomer(); if (customer == null) { return(RedirectToAction("SingIn", "Login")); } var cartList = db.ShoppingCartItems.Where(x => x.CustomerId == customer.Id).ToList(); decimal?totalprice = 0; ShoppingCartListModel model = new ShoppingCartListModel(); foreach (var item in cartList) { var product = db.Products.FirstOrDefault(x => x.Id == item.ProductId); var ShoppingCart = new ShoppingCartModel(); ShoppingCart.Id = item.Id; ShoppingCart.Quantity = item.Quantity; ShoppingCart.ProductName = product.Name; ShoppingCart.ProductId = product.Id; ShoppingCart.Price = product.Price; ShoppingCart.Photo = product.Photo; ShoppingCart.CustomerId = item.CustomerId; totalprice += product.Price * item.Quantity; model.ShoppingCarts.Add(ShoppingCart); } model.TotalPrice = totalprice; model.TotalQuantity = cartList.Count; return(PartialView("_ShoppingCart", model)); }
// GET: ShoppingCart public ActionResult Index() { var shoppingCarts = shoppingCartManager.SelectShoppingCarts(); ShoppingCartListModel model = ShoppingCartListModel.FromBusinessEntity(shoppingCarts); return(View(model)); }
/// <summary> /// Prepare paged shopping cart list model /// </summary> /// <param name="searchModel">Shopping cart search model</param> /// <returns>Shopping cart list model</returns> public virtual ShoppingCartListModel PrepareShoppingCartListModel(ShoppingCartSearchModel searchModel) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } //get customers with shopping carts var customers = _customerService.GetCustomersWithShoppingCarts(searchModel.ShoppingCartType, storeId: searchModel.StoreId, productId: searchModel.ProductId, createdFromUtc: searchModel.StartDate, createdToUtc: searchModel.EndDate, countryId: searchModel.BillingCountryId, pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize); //prepare list model var model = new ShoppingCartListModel { Data = customers.Select(customer => { //fill in model values from the entity var shoppingCartModel = new ShoppingCartModel { CustomerId = customer.Id }; //fill in additional values (not existing in the entity) shoppingCartModel.CustomerEmail = customer.IsRegistered() ? customer.Email : _localizationService.GetResource("Admin.Customers.Guest"); shoppingCartModel.TotalItems = _shoppingCartService.GetShoppingCart(customer, searchModel.ShoppingCartType, searchModel.StoreId, searchModel.ProductId, searchModel.StartDate, searchModel.EndDate).Sum(item => item.Quantity); return(shoppingCartModel); }), Total = customers.TotalCount }; return(model); }
/// <summary> /// Prepare paged shopping cart list model /// </summary> /// <param name="searchModel">Shopping cart search model</param> /// <returns>Shopping cart list model</returns> public virtual ShoppingCartListModel PrepareShoppingCartListModel(ShoppingCartSearchModel searchModel) { if (searchModel == null) { throw new ArgumentNullException(nameof(searchModel)); } //get customers with shopping carts var customers = _customerService.GetAllCustomers(loadOnlyWithShoppingCart: true, sct: searchModel.ShoppingCartType, pageIndex: searchModel.Page - 1, pageSize: searchModel.PageSize); //prepare list model var model = new ShoppingCartListModel { Data = customers.Select(customer => { //fill in model values from the entity var shoppingCartModel = new ShoppingCartModel { CustomerId = customer.Id }; //fill in additional values (not existing in the entity) shoppingCartModel.CustomerEmail = customer.IsRegistered() ? customer.Email : _localizationService.GetResource("Admin.Customers.Guest"); shoppingCartModel.TotalItems = customer.ShoppingCartItems .Where(item => item.ShoppingCartType == searchModel.ShoppingCartType).Sum(item => item.Quantity); return(shoppingCartModel); }), Total = customers.TotalCount }; return(model); }