public JsonResult Update(string cartModel) { var CusUserSession = (CusUserLogin)Session[CommonConstants.CusUserSession]; var jsonCart = new JavaScriptSerializer().Deserialize <List <CartItem> >(cartModel); if (CusUserSession != null) { var _sc = new ShoppingCartDAL().FindByCus(CusUserSession.CustomerID); if (_sc != null) { var _dal = new ShoppingCartDetailDAL(); var listSCDetail = new ShoppingCartDetailDAL().ListByShoppingCartID(_sc.ShoppingCartID); foreach (var item in listSCDetail) { var jsonItem = jsonCart.SingleOrDefault(x => x.Product.ProductID == item.Product.ProductID); if (jsonItem != null) { item.Quantity = jsonItem.Quantity; } var _result = _dal.Update(item); } } } else if (CusUserSession == null) { var sessionCart = (List <CartItem>)Session[CartSession]; foreach (var item in sessionCart) { var jsonItem = jsonCart.SingleOrDefault(x => x.Product.ProductID == item.Product.ProductID); if (jsonItem != null) { item.Quantity = jsonItem.Quantity; } } Session[CartSession] = sessionCart; } return(Json(new { status = true })); }
public ActionResult Edit([Bind(Include = "ShoppingCartDetailID,ShoppingCartID,ProductID,ProductName,UnitPrice,Quantity,PromotionPrice,CreateDate,CreateUser,ModifiedDate,ModifiedUser")] ShoppingCartDetail shoppingCartDetail) { var UserSession = (UserLogin)Session[TechDeviShopVs002.Common.CommonConstants.USER_SESSION]; if (ModelState.IsValid) { var _dal = new ShoppingCartDetailDAL(); shoppingCartDetail.ModifiedUser = UserSession.UserID; var _result = _dal.Update(shoppingCartDetail); if (_result) { return(RedirectToAction("Index", "ShoppingCartDetails")); } else { ModelState.AddModelError("", "Cập nhật chi tiết giỏ hàng ko thành công"); } } return(View(shoppingCartDetail)); }
public ActionResult AddItem(int productId, int quantity) { var CusUserSession = (CusUserLogin)Session[CommonConstants.CusUserSession]; var product = new ProductDAL().ViewDetail(productId); var cart = Session[CartSession]; if (CusUserSession == null) { if (cart != null) { var list = (List <CartItem>)cart; if (list.Exists(x => x.Product.ProductID == productId)) { foreach (var item in list) { if (item.Product.ProductID == productId) { item.Quantity += quantity; } } } else { //tạo mới đối tượng cart item var item = new CartItem(); item.Product = product; item.Quantity = quantity; list.Add(item); } //Gán vào session Session[CartSession] = list; } else { //tạo mới đối tượng cart item var item = new CartItem(); item.Product = product; item.Quantity = quantity; var list = new List <CartItem>(); list.Add(item); //Gán vào session Session[CartSession] = list; } } else if (CusUserSession != null) //cusUserSession != null; { var _sc = new ShoppingCartDAL().FindByCus(CusUserSession.CustomerID); if (_sc != null) { int _scID = _sc.ShoppingCartID; //var list = (List<CartItem>)cart; var _dal = new ShoppingCartDetailDAL(); //foreach (var item in list) //{ // _scID = item.ShoppingCartID; //} //if (list.Exists(x => x.Product.ProductID == productId)) //{ // foreach (var item in list) // { // if (item.Product.ProductID == productId) // { // item.Quantity += quantity; // } // } //} //else //{ // //tạo mới đối tượng cart item // var item = new CartItem(); // item.Product = product; // item.Quantity = quantity; // item.ShoppingCartID = _scID; // list.Add(item); //} var listSCDetail = new ShoppingCartDetailDAL().ListByShoppingCartID(_scID); if (listSCDetail.Exists(x => x.Product.ProductID == productId)) { foreach (var item in listSCDetail) { if (item.Product.ProductID == productId) { item.Quantity += quantity; } var _result = _dal.Update(item); } } else { //tạo mới shopping cartdetail var scDetailitem = new ShoppingCartDetail(); scDetailitem.ShoppingCartID = _scID; scDetailitem.ProductID = product.ProductID; scDetailitem.ProductName = product.ProductName; scDetailitem.Quantity = quantity; scDetailitem.UnitPrice = product.Price; scDetailitem.PromotionPrice = product.PromotionPrice; var _result = _dal.Insert(scDetailitem); } ////Gán vào session //Session[CartSession] = list; } else if (_sc == null)//cart == null { //tạo mới giỏ hàng, giỏ hàng chi tiết; var _shoppingCart = new ShoppingCart(); _shoppingCart.ShoppingDate = DateTime.Now; _shoppingCart.CustomerID = CusUserSession.CustomerID; _shoppingCart.CreateUser = CusUserSession.CustomerID; _shoppingCart.IsActive = true; var _scart = new ShoppingCartDAL().Insert(_shoppingCart); var _shoppingCartDetail = new ShoppingCartDetail(); _shoppingCartDetail.ShoppingCartID = _scart; _shoppingCartDetail.ProductID = product.ProductID; _shoppingCartDetail.ProductName = product.ProductName; _shoppingCartDetail.Quantity = quantity; _shoppingCartDetail.UnitPrice = product.Price; _shoppingCartDetail.PromotionPrice = product.PromotionPrice; var _scartDetail = new ShoppingCartDetailDAL().Insert(_shoppingCartDetail); ////tạo mới đối tượng cart item //var item = new CartItem(); //item.Product = product; //item.Quantity = quantity; //item.ShoppingCartID = _shoppingCart.ShoppingCartID; //var list = new List<CartItem>(); //list.Add(item); ////Gán vào session //Session[CartSession] = list; } } return(RedirectToAction("Index")); }