Example #1
0
        public ActionResult Add(Guid id)
        {
            Product eklenecekUrun = _productService.GetById(id);

            CartProductVM sepetUrunu = new CartProductVM
            {
                ID          = eklenecekUrun.ID,
                ProductName = eklenecekUrun.Name,
                UnitPrice   = eklenecekUrun.Price,
                Quantity    = 1
            };

            if (Session["sepet"] != null)
            {
                //Eğer sepet zaten varsa, sepeti yakala, gerçek tipine cast et, içine ürünü ekle, tekrar session içerisinde aynı yere son halini ekle.
                ProductCart cart = Session["sepet"] as ProductCart;
                cart.AddToCart(sepetUrunu);
                Session["sepet"] = cart;
            }
            else
            {
                //Eğer session içerisinde bir sepet yoksa, ilk kez sepet oluşuyordur. Sepeti oluştur ve ilk ürünü ekle.
                ProductCart cart = new ProductCart();
                cart.AddToCart(sepetUrunu);
                //İlk session oluşturmamda aşağıdaki kod kullanılır.
                //ilk parametre string tipte bu sessiondan tekrar yakalamakta kullancağımız isim(şu an sepet ismi verdik), ikinci parametre ise gerçek sepept nesnemizdir.
                Session.Add("sepet", cart);
            }
            //Ekleme işlemi sonucunda geriye json tipinde boş bile olsa değer dönmeliyiz. Eğer "return View()" bırakırsak , o zaman bize metodun kendi isminde bir view arayacak ve sayfamızı değiştirmeye çalışacaktır. Ajax sayesinde sayfanın yenilenmesini bile istemiyoruz. Bu nedenle bu metotlara bir view oluşturmuyoruz. Return View() yerine Return Json() yazıyoruz.
            ProductCart cart2 = Session["sepet"] as ProductCart;

            return(Json(cart2.CartProductList));
        }
Example #2
0
        public JsonResult Add(Guid id)
        {
            Product eklenecek = _productService.GetById(id);

            CartProductVM model = new CartProductVM
            {
                Id          = eklenecek.ID,
                ImagePath   = eklenecek.ImagePath,
                ProductName = eklenecek.Name,
                UnitPrice   = eklenecek.Price,
                Quantity    = 1
            };


            if (Session["sepet"] != null)
            {
                ProductCart cart = Session["sepet"] as ProductCart;
                cart.AddCart(model);

                Session["sepet"] = cart;
            }
            else
            {
                ProductCart cart = new ProductCart();

                cart.AddCart(model);
                Session.Add("sepet", cart);
            }


            return(Json(""));
        }
 public void AddCart(CartProductVM item)
 {
     if (!_cart.ContainsKey(item.ID))
     {
         _cart.Add(item.ID, item);
     }
     else
     {
         _cart[item.ID].Quantity++;
     }
 }
Example #4
0
 public void AddCart(CartProductVM item)
 {
     //Eğer sepette yoksa yeni ürün olarak ekle
     if (!_cart.ContainsKey(item.ID))
     {
         _cart.Add(item.ID, item);
     }
     else
     {
         //Eğer zaten ürün sepette varsa, miktarını arttır.
         _cart[item.ID].Quantity++;
     }
 }
Example #5
0
        public ActionResult Add(Guid id)
        {
            //gelen id'ye ait ürünü yakalıyoruz.
            Product sepeteEklenecekUrun = _productService.GetById(id);

            CartProductVM model = new CartProductVM
            {
                ID          = sepeteEklenecekUrun.ID,
                ProductCode = sepeteEklenecekUrun.ProductCode,
                Name        = sepeteEklenecekUrun.Name,
                Quantity    = 1
            };

            //Session Nedir ?
            //Server taraflı verileri tutmak için tasarlanmış bir yapıdır.
            //Kullanıcı oturumlarını, sepet vb yapıları client-server iletişimi sırasında saklamamıza yarar.
            //Session object tipinde değer tutar bu nedenle cast etmek gereklidir.
            if (Session["sepet"] != null)
            {
                //Eğer sepet varsa, session içerisinden o sepeti getiriyorum.
                ProductCart cart = Session["sepet"] as ProductCart;
                cart.AddCart(model);

                //Ürün eklendikten sonra yeni sepeti tekrar session'a atıyoruz
                Session["sepet"] = cart;
            }
            else
            {
                //eğer session içerisinde sepet yoksa yenisi oluşturulur.
                ProductCart cart = new ProductCart();
                cart.AddCart(model);
                Session.Add("sepet", cart);
                Session["sepet"] = cart;
            }

            //İsterseniz mesaj gönderebilirsiniz.
            return(Json("Sepete başarıyla eklendi!"));
        }
Example #6
0
        //Sepete Ekleme
        public ActionResult Add(Guid id)
        {
            //gelen idye ait ürünü yakalıyoruz.
            Product       sepeteEklenecekurun = _productService.GetById(id);
            CartProductVM model = new CartProductVM();

            model.ID          = sepeteEklenecekurun.ID;
            model.ProductName = sepeteEklenecekurun.Name;
            model.UnitPrice   = sepeteEklenecekurun.Price;
            model.Quantity    = 1;


            //Session Nedir ?
            //Server taraflı verileri tutmak için tasarlanmış bir yapıdır.
            //Kullanıcı oturumlarını, sepet vb yapıları client-server iletişimi sırasında saklamamıza yarar.
            //Session object tipinde değer tutar bu nedenle cast etmek gereklidir.
            if (Session["sepet"] != null)
            {
                //Session içerisinde bir sepet varsa
                ProductCart cart = Session["sepet"] as ProductCart;
                cart.AddCart(model);
                //Ürün eklendikten sonra sepetin son halini sessiona at
                Session["sepet"] = cart;
            }
            else
            {
                //Session içerisinde bir sepet yoksa
                ProductCart cart = new ProductCart();
                cart.AddCart(model);
                //Session'ı ilk kez oluşturuyoruz.
                Session.Add("sepet", cart);
            }

            ProductCart cartSon = Session["sepet"] as ProductCart;

            return(Json(cartSon.CartProductList.Count));
        }