コード例 #1
0
        public IActionResult Add(int id)
        {
            var sessionCart = HttpContext.Session.GetString("CurrentShoppingCart");

            if (sessionCart != null)
            {
                ShoppingCart        shoppingCart        = _unitOfWork.ShoppingCarts.GetById(int.Parse(sessionCart));
                Product             product             = _unitOfWork.Products.GetById(id);
                ProductShoppingCart productShoppingCart = _unitOfWork.ProductShoppingCarts.GetAll().FirstOrDefault(p => p.Product.Id == product.Id);

                if (productShoppingCart != null)
                {
                    productShoppingCart.Amount++;
                }
                else
                {
                    shoppingCart.ProductShoppingCarts.Add(
                        new ProductShoppingCart {
                        Product = product, ShoppingCart = shoppingCart, Amount = 1
                    });
                }
                _unitOfWork.Complete();
            }

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        public string Add(int ProductCode, int Quantity)
        {
            ACMEDbContext context = new ACMEDbContext();
            string        email   = HttpContext.Session.GetString("Email");
            var           user    = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First();
            ShoppingCart  cart    = user.ShoppingCart;

            //Check if user already has item in cart
            if (context.ProductShoppingCarts.Any(psc => psc.Product.ProductCode == ProductCode && psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID))
            {
                //Add quantity to current product shopping cart entry
                ProductShoppingCart entry = context.ProductShoppingCarts.First(psc => psc.Product.ProductCode == ProductCode && psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID);
                entry.Quantity += Quantity;
            }
            else
            {
                // create new entry
                ProductShoppingCart productShoppingCart = new ProductShoppingCart
                {
                    Product      = context.Products.Find(ProductCode),
                    ShoppingCart = cart,
                    Quantity     = Quantity
                };
                context.ProductShoppingCarts.Add(productShoppingCart);
            }

            context.SaveChanges();
            return("Ok");
        }
コード例 #3
0
        public string Edit(int ProductCode, int Quantity)
        {
            ACMEDbContext       context = new ACMEDbContext();
            string              email   = HttpContext.Session.GetString("Email");
            var                 user    = context.Users.Where(u => u.Email == email).Include(u => u.ShoppingCart).First();
            ShoppingCart        cart    = user.ShoppingCart;
            ProductShoppingCart entry   = context.ProductShoppingCarts.First(psc => psc.ShoppingCart.ShoppingCartID == cart.ShoppingCartID && psc.Product.ProductCode == ProductCode);

            entry.Quantity = Quantity;
            context.SaveChanges();
            return("Ok");
        }
コード例 #4
0
        public async Task <bool> AddToCart(string productId, string deviceId, string userId)
        {
            var cart = await GetCurrentCartForUser(deviceId, userId);

            if (cart == null)
            {
                cart            = new Models.ShoppingCart();
                cart.ClientGuid = deviceId;

                if (userId != null)
                {
                    try
                    {
                        cart.User = _context.Users.SingleOrDefault(u => u.Id == Int32.Parse(userId));
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }
                }

                _context.ShoppingCarts.Add(cart);
            }

            var item = _context.ProductShoppingCarts.SingleOrDefault(
                pc => pc.ShoppingCartId == cart.ShoppingCartId && pc.ProductId == Int32.Parse(productId)
                );

            if (item == null)
            {
                item                = new ProductShoppingCart();
                item.ProductId      = Int32.Parse(productId);
                item.ShoppingCartId = cart.ShoppingCartId;
                cart.ProductShoppingCarts.Add(item);

                await _context.SaveChangesAsync();
            }

            return(true);
        }
コード例 #5
0
        public ActionResult AddToCart(ProductDetailViewModel viewModel, string btnSubmit)
        {
            if (Session["Carts"] == null)
            {
                Session["Carts"] = new List <ProductShoppingCart>();
            }
            ProductShoppingCart cart = Mapper.Map <ProductDetailViewModel, ProductShoppingCart>(viewModel);
            var carts = (List <ProductShoppingCart>)Session["Carts"];
            var c     = carts.Where(x => x.Id == cart.Id).FirstOrDefault();

            cart.BuyingQuantity = 1;
            if (c != null)
            {
                c.BuyingQuantity++;
                c.TotalPrice = (c.BuyingQuantity * double.Parse((c.PromotionalPrice == "1" ? c.Price : c.PromotionalPrice).Replace(".", ""))).ToString();
                c.TotalPrice = PriceHelper.NormalizePrice(c.TotalPrice);
                if (c.Price == "1")
                {
                    c.TotalPrice = "Giá bán liên hệ";
                }
            }
            else
            {
                cart.TotalPrice = cart.PromotionalPrice == "1" ? cart.Price : cart.PromotionalPrice;
                if (cart.Price == "1")
                {
                    cart.TotalPrice = "Giá bán liên hệ";
                }
                carts.Add(cart);
            }
            if (btnSubmit == "Mua ngay")
            {
                return(RedirectToAction("PlaceOrder"));
            }
            return(RedirectToAction("Index"));
        }