public void AddProductToCart(ISession session, CartItemVm addProductToCart) { var sessionData = this.GetSessionCartData(session); var existProduct = sessionData.Products.FirstOrDefault(x => x.ProductId == addProductToCart.ProductId); if (existProduct != default) { existProduct.Quantity++; } else { sessionData.Products.Add(new CartDetailsItemSessionVm { Id = addProductToCart.Id, ProductId = addProductToCart.ProductId, ProductName = addProductToCart.ProductName, ImageUrl = addProductToCart.ImageUrl, Quantity = addProductToCart.Quantity, Price = addProductToCart.Price }); } this.SaveSessionCartData(session, sessionData); }
// TODO separate getting product thumbnail, varation options from here public async Task <CartVm> GetActiveCartDetails(long customerId, long createdById) { var cart = await GetActiveCart(customerId, createdById); if (cart == null) { return(null); } var cartVm = new CartVm() { Id = cart.Id, CouponCode = cart.CouponCode, IsProductPriceIncludeTax = cart.IsProductPriceIncludeTax, TaxAmount = cart.TaxAmount, ShippingAmount = cart.ShippingAmount, OrderNote = cart.OrderNote }; cartVm.Items = _cartItemRepository .Query() .Include(x => x.Product).ThenInclude(p => p.ThumbnailImage) .Include(x => x.Product).ThenInclude(p => p.OptionCombinations).ThenInclude(o => o.Option) .Where(x => x.CartId == cart.Id) .Select(x => new CartItemVm { Id = x.Id, ProductId = x.ProductId, ProductName = x.Product.Name, ProductPrice = x.Product.Price, ProductImage = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage), Quantity = x.Quantity, VariationOptions = CartItemVm.GetVariationOption(x.Product) }).ToList(); cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice); if (!string.IsNullOrWhiteSpace(cartVm.CouponCode)) { var cartInfoForCoupon = new CartInfoForCoupon { Items = cartVm.Items.Select(x => new CartItemForCoupon { ProductId = x.ProductId, Quantity = x.Quantity }).ToList() }; var couponValidationResult = await _couponService.Validate(customerId, cartVm.CouponCode, cartInfoForCoupon); if (couponValidationResult.Succeeded) { cartVm.Discount = couponValidationResult.DiscountAmount; } else { cartVm.CouponValidationErrorMessage = couponValidationResult.ErrorMessage; } } return(cartVm); }
public void RemoveProductFromCart(ISession session, CartItemVm removeProductFromCart) { var sessionData = this.GetSessionCartData(session); var removeItem = sessionData.Products.FirstOrDefault(x => x.Id == removeProductFromCart.Id); sessionData.Products.Remove(removeItem); this.SaveSessionCartData(session, sessionData); }
public IActionResult RemoveFromCart(int id) { var removeProductFromCart = new CartItemVm { Id = id }; _cartService.RemoveProductFromCart(HttpContext.Session, removeProductFromCart); TempData["SM"] = "Вы успешно удалили."; return(RedirectToAction("Index", "Cart")); }
public async Task <IActionResult> Index() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); List <CartItemVm> ListProduct = new(); CartVm cartVm = new() { UserId = userId }; if (!User.Identity.IsAuthenticated) { ListProduct = HttpContext.Session.Get <List <CartItemVm> >("SessionCart"); } else { cartVm = await _cartApiClient.GetCartByUser(userId); var lstCartItem = cartVm.cartItemVms.ToList(); var lstProduct = new List <CartItemVm>(); if (lstCartItem.Count > 0) { foreach (var x in lstCartItem) { var pVm = new CartItemVm() { productVm = new ProductVm(), }; pVm.productVm.CategoryId = x.productVm.CategoryId; pVm.productVm.Description = x.productVm.Description; pVm.productVm.Id = x.Id; pVm.productVm.ImageLocation = x.productVm.ImageLocation; pVm.productVm.Inventory = x.productVm.Inventory; pVm.productVm.Name = x.productVm.Name; pVm.productVm.Price = x.productVm.Price; pVm.Quantity = x.Quantity; pVm.productVm.AverageStar = x.productVm.AverageStar; lstProduct.Add(pVm); } ; } return(View(lstProduct)); } if (ListProduct == null) { return(NotFound()); } return(View(ListProduct)); }
// TODO separate getting product thumbnail, varation options from here public async Task <CartVm> GetCart(long userId) { var cart = _cartRepository.Query().FirstOrDefault(x => x.UserId == userId && x.IsActive); if (cart == null) { return(new CartVm()); } var cartVm = new CartVm() { Id = cart.Id, CouponCode = cart.CouponCode, TaxAmount = cart.TaxAmount, ShippingAmount = cart.ShippingAmount }; cartVm.Items = _cartItemRepository .Query() .Include(x => x.Product).ThenInclude(p => p.ThumbnailImage) .Include(x => x.Product).ThenInclude(p => p.OptionCombinations).ThenInclude(o => o.Option) .Where(x => x.CartId == cart.Id) .Select(x => new CartItemVm { Id = x.Id, ProductId = x.ProductId, ProductName = x.Product.Name, ProductPrice = x.Product.Price, ProductImage = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage), Quantity = x.Quantity, VariationOptions = CartItemVm.GetVariationOption(x.Product) }).ToList(); cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice); if (!string.IsNullOrWhiteSpace(cartVm.CouponCode)) { var cartInfoForCoupon = new CartInfoForCoupon { Items = cartVm.Items.Select(x => new CartItemForCoupon { ProductId = x.ProductId, Quantity = x.Quantity }).ToList() }; var couponValidationResult = await _couponService.Validate(cartVm.CouponCode, cartInfoForCoupon); if (couponValidationResult.Succeeded) { cartVm.Discount = couponValidationResult.DiscountAmount; } } return(cartVm); }
public async Task <ActionResult <CartVm> > GetCartByUser(string userId) { var cart = await _context.Carts.Include(x => x.CartItems) .ThenInclude(x => x.Product).ThenInclude(x => x.Images).FirstOrDefaultAsync(x => x.UserId == userId); if (cart == null) { return(NotFound()); } var lstProduct = cart.CartItems.ToList(); var cartItemVms = new List <CartItemVm>(); var c = new ProductVm(); foreach (var x in lstProduct.ToList()) { var cartItemVm = new CartItemVm(); var lstImage = new List <string>(); c = new ProductVm { CategoryId = x.Product.CategoryId, Description = x.Product.Description, Id = x.Product.Id, Inventory = x.Product.Inventory, Name = x.Product.Name, Price = x.Product.Price, }; foreach (var y in x.Product.Images.ToList()) { lstImage.Add(y.ImagePath.ToString()); } c.ImageLocation = lstImage; cartItemVm.Quantity = x.Quantity; cartItemVm.productVm = c; cartItemVms.Add(cartItemVm); } var cartVm = new CartVm { Id = cart.Id, TotalPrice = cart.TotalPrice, UserId = cart.UserId, cartItemVms = cartItemVms }; return(cartVm); }
public IActionResult AddToCart(int productId, int?quantity) { var product = _productRepository.GetByIdAsync(productId).Result; var addProductToCart = new CartItemVm { Id = productId, ProductId = productId, ProductName = product.Name, Price = product.Price, ImageUrl = product.CoverImageUrl, Quantity = quantity ?? 1 }; _cartService.AddProductToCart(HttpContext.Session, addProductToCart); TempData["SM"] = "Товар добавлен"; return(RedirectToAction("Details", "Product", new { id = productId })); }
public async Task <ActionResult <CartVm> > GetCartById(int id) { var cart = await _context.Carts.FindAsync(id); if (cart == null) { return(NotFound()); } var lstProduct = cart.CartItems.ToList(); var lstImage = new List <string>(); var cartItemVms = new List <CartItemVm>(); var cartItemVm = new CartItemVm(); foreach (var x in lstProduct) { var c = new ProductVm { CategoryId = x.Product.CategoryId, Description = x.Product.Description, Id = x.Id, Inventory = x.Product.Inventory, Name = x.Product.Name, Price = x.Product.Price, }; foreach (var y in c.ImageLocation) { lstImage.Add(y); } c.ImageLocation = lstImage; cartItemVm.productVm = c; cartItemVms.Add(cartItemVm); } var cartVm = new CartVm { Id = cart.Id, TotalPrice = cart.TotalPrice, UserId = cart.UserId, cartItemVms = cartItemVms }; return(cartVm); }
public async Task <ActionResult <IEnumerable <CartVm> > > GetAllCart() { var lstCartItem = new List <CartItem>(); var lstCartItemVm = new List <CartItemVm>(); var lstImage = new List <string>(); foreach (var x in _context.Carts.Select(x => x.CartItems)) { lstCartItem = x.ToList(); } var cartItemVm = new CartItemVm(); var lstProductVm = new List <ProductVm>(); foreach (var x in lstCartItem) { x.Product = new Product(); var c = new ProductVm { CategoryId = x.Product.CategoryId, Description = x.Product.Description, Id = x.Id, Inventory = x.Product.Inventory, Name = x.Product.Name, Price = x.Product.Price, }; cartItemVm.productVm = new ProductVm(); cartItemVm.productVm = c; lstCartItemVm.Add(cartItemVm); } var cartVm = await _context.Carts .Select(x => new CartVm { Id = x.Id, TotalPrice = x.TotalPrice, UserId = x.UserId, cartItemVms = lstCartItemVm }) .ToListAsync(); return(cartVm); }
public CartVm DataCarrito() { //GetStructCarrito(GetIP(), GetSession()); var blc = RecuperaTotalCarrito(GetIP(), GetSession()); var todas = RecuperatodasLineasCarrito(GetIP(), GetSession()); Random r = new Random(); // int totalLineas = int.Parse(blc.totallines); var cartVm = new CartVm(_currencyService) { Id = r.Next(0, 60000), //int.Parse(DateTime.Now.ToString()), IsProductPriceIncludeTax = true //TaxAmount = cart.TaxAmount, //ShippingAmount = cart.ShippingAmount, //OrderNote = cart.OrderNote }; foreach (CartLine ln in todas.result) { CartItemVm civ = new CartItemVm(_currencyService); civ.Id = long.Parse(ln.line.ToString()); civ.ProductId = long.Parse(ln.identifier); var arti = RecuperaArtículo(GetIP(), GetSession(), civ.ProductId); int sta = 0; _ = int.TryParse(arti.result.stocks, out sta); civ.ProductStockQuantity = decimal.ToInt32(decimal.Parse(arti.result.stocks.Replace(".", ","))); civ.ProductName = ln.description; civ.ProductPrice = decimal.Parse(ln.pricewithtax.Replace(".", ",")); civ.IsProductAvailabeToOrder = true; civ.ProductStockTrackingIsEnabled = false; //Core.Models.Media pti = new ProductThumbnail().; civ.ProductImage = ln.imagesmall; civ.Quantity = decimal.ToInt32(decimal.Parse(ln.quantity)); cartVm.Items.Add(civ); } cartVm.SubTotal = decimal.Parse(blc.totalwithtax.Replace(".", ",")); cartVm.Discount = 0; return(cartVm); }
public async Task <IActionResult> Index() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (!User.Identity.IsAuthenticated) { return(RedirectToAction("SignIn", "Account")); } else { var cartVm = await _cartApiClient.GetCartByUser(userId); cartVm.UserId = userId; var lstCartItem = cartVm.cartItemVms.ToList(); var lstProduct = new List <CartItemVm>(); if (lstCartItem.Count > 0) { foreach (var x in lstCartItem) { var pVm = new CartItemVm() { productVm = new ProductVm(), }; pVm.productVm.CategoryId = x.productVm.CategoryId; pVm.productVm.Description = x.productVm.Description; pVm.productVm.Id = x.Id; pVm.productVm.ImageLocation = x.productVm.ImageLocation; pVm.productVm.Inventory = x.productVm.Inventory; pVm.productVm.Name = x.productVm.Name; pVm.productVm.Price = x.productVm.Price; pVm.Quantity = x.Quantity; pVm.productVm.AverageStar = x.productVm.AverageStar; lstProduct.Add(pVm); } ; } return(View(lstProduct)); } }
public IActionResult Cart() { List <CartItem> cartItem = HttpContext.Session.Get <List <CartItem> >("cart"); List <CartItemVm> addItem = new List <CartItemVm>(); if (cartItem != null) { foreach (var item in cartItem) { CartItemVm itemVm = new CartItemVm(); var cartProdut = _context.Products.Include(c => c.Category).FirstOrDefault(c => c.Id == item.ProductId); itemVm.ProductId = item.ProductId; itemVm.Quentity = item.Quantity; itemVm.Name = cartProdut.Name; itemVm.Price = cartProdut.Price; itemVm.PrevPrice = cartProdut.PrevPrice; itemVm.Category = cartProdut.Category.Name; itemVm.Image = cartProdut.Image; addItem.Add(itemVm); } } return(View(addItem)); }
public CartVm DataCarrito() { //GetStructCarrito(GetIP(), GetSession()); var blc = RecuperaTotalCarrito(GetIP(), GetSession()); var todas = RecuperatodasLineasCarrito(GetIP(), GetSession()); Random r = new Random(); // int totalLineas = int.Parse(blc.totallines); var cartVm = new CartVm(_currencyService) { Id = r.Next(0, 60000), //int.Parse(DateTime.Now.ToString()), IsProductPriceIncludeTax = true //TaxAmount = cart.TaxAmount, //ShippingAmount = cart.ShippingAmount, //OrderNote = cart.OrderNote }; foreach (CartLine ln in todas.result) { CartItemVm civ = new CartItemVm(_currencyService); civ.Id = long.Parse(ln.line.ToString()); civ.ProductId = long.Parse(ln.identifier); var arti = RecuperaArtículo(GetIP(), GetSession(), civ.ProductId); int sta = 0; _ = int.TryParse(arti.result.stocks, out sta); civ.ProductStockQuantity = decimal.ToInt32(decimal.Parse(arti.result.stocks)); civ.ProductName = ln.description; civ.ProductPrice = decimal.Parse(ln.pricewithtax.Replace(".", ",")); civ.IsProductAvailabeToOrder = true; civ.ProductStockTrackingIsEnabled = false; civ.linea = ln.line; //Core.Models.Media pti = new ProductThumbnail().; civ.ProductImage = ln.imagesmall; civ.Quantity = decimal.ToInt32(decimal.Parse(ln.quantity.Replace(".", ","))); // Id = x.Id, // ProductId = x.ProductId, // ProductName = x.Product.Name, // ProductPrice = x.Product.Price, // ProductStockQuantity = x.Product.StockQuantity, // ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled, // IsProductAvailabeToOrder = x.Product.IsAllowToOrder && x.Product.IsPublished && !x.Product.IsDeleted, // ProductImage = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage), // Quantity = x.Quantity, cartVm.Items.Add(civ); } cartVm.SubTotal = decimal.Parse(blc.totalwithtax.Replace(".", ",")); cartVm.Discount = 0; return(cartVm); //Datos esperados, los mismos que devuelve await _cartService.GetActiveCartDetails(currentUser.Id); //var cartVm = new CartVm(_currencyService) //{ // Id = cart.Id, // CouponCode = cart.CouponCode, // IsProductPriceIncludeTax = cart.IsProductPriceIncludeTax, // TaxAmount = cart.TaxAmount, // ShippingAmount = cart.ShippingAmount, // OrderNote = cart.OrderNote //}; //cartVm.Items = _cartItemRepository // .Query() // .Include(x => x.Product).ThenInclude(p => p.ThumbnailImage) // .Include(x => x.Product).ThenInclude(p => p.OptionCombinations).ThenInclude(o => o.Option) // .Where(x => x.CartId == cart.Id).ToList() // .Select(x => new CartItemVm(_currencyService) // { // Id = x.Id, // ProductId = x.ProductId, // ProductName = x.Product.Name, // ProductPrice = x.Product.Price, // ProductStockQuantity = x.Product.StockQuantity, // ProductStockTrackingIsEnabled = x.Product.StockTrackingIsEnabled, // IsProductAvailabeToOrder = x.Product.IsAllowToOrder && x.Product.IsPublished && !x.Product.IsDeleted, // ProductImage = _mediaService.GetThumbnailUrl(x.Product.ThumbnailImage), // Quantity = x.Quantity, // VariationOptions = CartItemVm.GetVariationOption(x.Product) // }).ToList(); //cartVm.SubTotal = cartVm.Items.Sum(x => x.Quantity * x.ProductPrice); //if (!string.IsNullOrWhiteSpace(cartVm.CouponCode)) //{ // var cartInfoForCoupon = new CartInfoForCoupon // { // Items = cartVm.Items.Select(x => new CartItemForCoupon { ProductId = x.ProductId, Quantity = x.Quantity }).ToList() // }; // var couponValidationResult = await _couponService.Validate(customerId, cartVm.CouponCode, cartInfoForCoupon); // if (couponValidationResult.Succeeded) // { // cartVm.Discount = couponValidationResult.DiscountAmount; // } // else // { // cartVm.CouponValidationErrorMessage = couponValidationResult.ErrorMessage; // } //} }