Ejemplo n.º 1
0
        public ActionResult Index()
        {
            var list = new List <CartItem>();

            if (User.Identity.IsAuthenticated == false)
            {
                HttpCookie Cookie = HttpContext.Request.Cookies[ListCart.CartCookie];                   // lấy cookie

                if (Cookie != null)                                                                     // check cookies có value nếu cookie not null
                {
                    string ValueCookie = Server.UrlDecode(Cookie.Value);                                //Decode dịch ngược mã  các ký tự đặc biệt tham khảo
                    var    cart        = JsonConvert.DeserializeObject <List <CartItem> >(ValueCookie); // convert json to  list object
                    if (cart != null)
                    {
                        list = (List <CartItem>)cart;
                    }

                    int i = 0;
                    while (1 != 2)
                    {
                        i++;
                        HttpCookie cooki = HttpContext.Request.Cookies[i.ToString()];// lấy cookie
                        if (cooki != null)
                        {
                            string valueCookie = Server.UrlDecode(cooki.Value);                                 //Decode dịch ngược mã  các ký tự đặc biệt tham khảo
                            var    cart2       = JsonConvert.DeserializeObject <List <CartItem> >(valueCookie); // convert json to  list object
                            if (cart2 != null)
                            {
                                var list2 = (List <CartItem>)cart2;

                                list.AddRange(list2);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                var orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId());
                if (orderDetailTemp != null)
                {
                    string Value = Server.UrlDecode(orderDetailTemp.CartContent);           //Decode dịch ngược mã  các ký tự đặc biệt tham khảo
                    var    cart  = JsonConvert.DeserializeObject <List <CartItem> >(Value); // convert json to  list object
                    list = (List <CartItem>)cart;
                }
            }



            return(View(list));
        }
Ejemplo n.º 2
0
        public ActionResult Index()
        {
            ViewBag.TopHotALL = _productService.ListTopHotALL();

            ViewBag.productCategory = _productCategoryService.ListAll();

            var list = new List <CartItem>();

            try
            {
                HttpCookie Cookie = HttpContext.Request.Cookies[ListCart.CartCookie];                   // lấy cookie

                if (Cookie != null)                                                                     // check cookies có value nếu cookie not null
                {
                    string ValueCookie = Server.UrlDecode(Cookie.Value);                                //Decode dịch ngược mã  các ký tự đặc biệt tham khảo
                    var    cart        = JsonConvert.DeserializeObject <List <CartItem> >(ValueCookie); // convert json to  list object
                    if (cart != null)
                    {
                        list = (List <CartItem>)cart;
                    }

                    int kk = 0;
                    while (1 != 2)
                    {
                        kk++;
                        HttpCookie cooki = HttpContext.Request.Cookies[kk.ToString()];// lấy cookie
                        if (cooki != null)
                        {
                            string valueCookie = Server.UrlDecode(cooki.Value);                                 //Decode dịch ngược mã  các ký tự đặc biệt tham khảo
                            var    cart2       = JsonConvert.DeserializeObject <List <CartItem> >(valueCookie); // convert json to  list object
                            if (cart2 != null)
                            {
                                var list2 = (List <CartItem>)cart2;

                                list.AddRange(list2);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch { }



            ////////
            //
            if (User.Identity.IsAuthenticated && list.Count != 0)
            {
                foreach (var lst in list)
                {
                    var orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId());
                    var product         = _productService.GetById(lst.Product.Id);
                    if (orderDetailTemp == null)
                    {
                        OrderDetailTemp temp = new OrderDetailTemp();
                        temp.Id     = Guid.NewGuid();
                        temp.UserID = User.Identity.GetUserId();
                        _orderDetailTempService.Create(temp);
                        orderDetailTemp = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId());
                    }
                    if (orderDetailTemp.CartContent == null)
                    {
                        var item = new CartItem();
                        item.Product = product;
                        item.Quatity = lst.Quatity;
                        var list2 = new List <CartItem>();
                        list2.Add(item);

                        string jsonItem = JsonConvert.SerializeObject(list2, Formatting.Indented);

                        var value = Server.UrlEncode(jsonItem); //Encode dịch  mã  các ký tự đặc biệt, từ string
                        orderDetailTemp.CartContent = value;    // cookie mới đè lên cookie cũ
                    }
                    else
                    {
                        string value = Server.UrlDecode(orderDetailTemp.CartContent);           //Decode dịch ngược mã  các ký tự đặc biệt tham khảo
                        var    cart  = JsonConvert.DeserializeObject <List <CartItem> >(value); // convert json to  list object
                        var    list2 = (List <CartItem>)cart;
                        if (list2.Exists(x => x.Product.Id == product.Id))
                        {
                            foreach (var item in list2)
                            {
                                if (item.Product.Id == product.Id)
                                {
                                    item.Quatity += lst.Quatity;
                                }
                            }
                        }
                        else
                        {
                            var item = new CartItem();
                            item.Product = product;
                            item.Quatity = lst.Quatity;

                            list2.Add(item);
                        }

                        string          jsonItem = JsonConvert.SerializeObject(list2, Formatting.Indented);
                        OrderDetailTemp temp     = new OrderDetailTemp();
                        temp.Id          = _orderDetailTempService.ListAllByUserID(User.Identity.GetUserId()).Id;
                        temp.UserID      = User.Identity.GetUserId();
                        temp.CartContent = jsonItem;
                        _orderDetailTempService.Update(temp);
                    }
                }

                /////Xóa cooki
                ///
                int i = -1;
                while (1 != 2)
                {
                    i++;
                    HttpCookie cooki = HttpContext.Request.Cookies[i.ToString()];// lấy cookie
                    if (cooki == null)
                    {
                        break;
                    }
                    //cooki.Expires = DateTime.Now.AddDays(-100);
                    //HttpContext.Response.Cookies.Add(cooki);
                    HttpContext.Response.Cookies[i.ToString()].Expires = DateTime.Now.AddDays(-1);
                }
            }


            return(View());
        }