Exemplo n.º 1
0
        public ActionResult Add(int?productID)
        {
            // then didnt come from products page
            if (productID == null)
            {
                return(RedirectToAction("Index")); // send them there
            }
            // used to send current qunatity to the page
            string           sessionID = System.Web.HttpContext.Current.Session.SessionID;
            ShoppingCartRepo cartRepo  = new ShoppingCartRepo();
            ProductVisit     cartItem  = cartRepo.GetCartItem(sessionID, (int)productID);

            ProductVM productVM;

            if (cartItem == null)
            {
                ProductRepo pRepo   = new ProductRepo();
                Product     product = pRepo.GetProduct(productID);

                productVM = new ProductVM(product);
                productVM.SetTotalCost(); // quntity times price
                productVM.image    = product.Image.imageTitle;
                productVM.quantity = 1;
            }
            else
            {
                productVM = new ProductVM(cartItem.Product);
                productVM.SetTotalCost(); // quntity times price
                productVM.image    = cartItem.Product.Image.imageTitle;
                productVM.quantity = cartItem.qtyOrdered;
            }

            return(View(productVM));
        }
Exemplo n.º 2
0
        public ActionResult ViewCart(int?id)
        {
            string sessionID = System.Web.HttpContext.Current.Session.SessionID;

            if (id != null)
            {
                // remove product visit
                ProductVisitRepo productVisitRepo = new ProductVisitRepo();
                productVisitRepo.RemoveProductVisit(sessionID, (int)id);
            }

            ShoppingCartRepo cartRepo = new ShoppingCartRepo();
            // get all product visit entries
            IEnumerable <ProductVisit> productVisits = cartRepo.GetCartItems(sessionID);

            // if have product visit can create a cart item out of it via the naviagtion properties
            List <ProductVM> products = new List <ProductVM>();

            foreach (ProductVisit item in productVisits)
            {
                ProductVM product = new ProductVM(item.Product, (int)item.qtyOrdered);
                product.SetTotalCost();
                product.image = item.Product.Image.imageTitle;
                products.Add(product);
            }

            ShoppingCartVM cart = new ShoppingCartVM(products);

            if (products.Count() > 0)
            {
                return(View(cart));
            }
            else
            {
                ViewBag.Message = "No cart items selected";
                return(View(cart));
            }
        }