/// <summary> /// 添加到购物车 /// </summary> /// <param name="productId"></param> /// <param name="sum"></param> /// <returns></returns> public JsonResult AddCart(int productId, int sum) { int count = 0; var product = _productservice.GetProductById(productId); if (product == null) { return(Json(0, JsonRequestBehavior.AllowGet)); } ShopCart cart = new ShopCart(); if (assets.MemberId > 0) { cart = _shopcartservice.GetCartByProductId(assets.MemberId, productId); } else { cart = _shopcartservice.GetCartByProductId(assets.CookieTag, productId); } if (cart == null) { //添加新品到购物车 } else { //更新购物车 } //查找购物车数量 return(Json(count, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 添加到购物车 /// </summary> /// <param name="productId"></param> /// <param name="sum"></param> /// <returns></returns> public JsonResult AddCart(int productId, int sum) { int count = 0; var product = _productservice.GetProductById(productId); if (product == null) { return(Json(0, JsonRequestBehavior.AllowGet)); } ShopCart cart = new ShopCart(); if (assets.MemberId > 0) { cart = _shopcartservice.GetCartByProductId(assets.MemberId, productId); } else { cart = _shopcartservice.GetCartByProductId(assets.CookieTag, productId); } if (cart != null) { //添加新品到购物车 ShopCart newcart = new ShopCart(); newcart.CookieTag = assets.CookieTag; newcart.MemberId = assets.MemberId; newcart.CreateTime = DateTime.Now; newcart.Price = product.Price; newcart.TotPrice = product.Price * sum; newcart.ProductNum = sum; newcart.ProductPic = product.Cover; newcart.ProductId = product.Id; int cartid = _shopcartservice.InsertCart(newcart); } else { //更新购物车 cart.ProductNum += sum; cart.Price += product.Price; cart.TotPrice = cart.ProductNum * cart.Price; _shopcartservice.UpdateCart(cart); } if (assets.MemberId > 0) { count = _shopcartservice.GetCountByMemberId(assets.MemberId); } else { count = _shopcartservice.GetCartCountByCookie(assets.CookieTag); } //查找购物车数量 return(Json(count, JsonRequestBehavior.AllowGet)); }