/// <summary> /// 获取购物车中的商品 /// </summary> /// <returns></returns> public IEnumerable <string> GetCartProductSKUIds(long memberId) { string[] productIds = new string[] { }; if (memberId > 0)//已经登录,系统从服务器读取购物车信息,否则从Cookie获取购物车信息 { var cartInfo = CartApplication.GetCart(memberId); productIds = cartInfo.Items.Select(item => item.SkuId).ToArray(); } else { string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART); if (!string.IsNullOrWhiteSpace(cartInfo)) { string[] cartItems = cartInfo.Split(','); productIds = new string[cartItems.Length]; int i = 0; foreach (string cartItem in cartItems) { var cartItemParts = cartItem.Split(':'); productIds[i++] = cartItemParts[0];//获取商品SKUId } } } return(productIds); }
/// <summary> /// 获取购物车中的商品 /// </summary> /// <returns></returns> public Himall.Entities.ShoppingCartInfo GetCart(long memberId) { Himall.Entities.ShoppingCartInfo shoppingCartInfo; if (memberId > 0)//已经登录,系统从服务器读取购物车信息,否则从Cookie获取购物车信息 { shoppingCartInfo = CartApplication.GetCart(memberId); } else { shoppingCartInfo = new Himall.Entities.ShoppingCartInfo(); string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART); if (!string.IsNullOrWhiteSpace(cartInfo)) { string[] cartItems = cartInfo.Split(','); var cartInfoItems = new List <Himall.Entities.ShoppingCartItem>(); foreach (string cartItem in cartItems) { var cartItemParts = cartItem.Split(':'); cartInfoItems.Add(new Himall.Entities.ShoppingCartItem() { ProductId = long.Parse(cartItemParts[0].Split('_')[0]), SkuId = cartItemParts[0], Quantity = int.Parse(cartItemParts[1]) }); } shoppingCartInfo.Items = cartInfoItems; } } return(shoppingCartInfo); }
public void AddToCart(string skuId, int count, long memberId) { CheckSkuIdIsValid(skuId); //判断库存 var sku = ProductManagerApplication.GetSKU(skuId); if (sku == null) { throw new HimallException("错误的SKU"); } if (count > sku.Stock) { throw new HimallException("库存不足"); } #region 商品限购判断 var prouctInfo = ProductManagerApplication.GetProduct(sku.ProductId); if (prouctInfo != null && prouctInfo.MaxBuyCount > 0 && !prouctInfo.IsOpenLadder)//商品有限购数量 { var carInfo = CartApplication.GetCart(memberId); if (carInfo != null) { var quantity = carInfo.Items.Where(p => p.ProductId == sku.ProductId).Sum(d => d.Quantity); //当前用户该商品已加入购物车数量 if (count + quantity > prouctInfo.MaxBuyCount) //已有数量+新数量 { throw new HimallException(string.Format("每个ID限购{0}件", prouctInfo.MaxBuyCount)); } } } #endregion if (memberId > 0) { CartApplication.AddToCart(skuId, count, memberId); } else { string cartInfo = WebHelper.GetCookie(CookieKeysCollection.HIMALL_CART); if (!string.IsNullOrWhiteSpace(cartInfo)) { string[] cartItems = cartInfo.Split(','); string newCartInfo = string.Empty; bool exist = false; foreach (string cartItem in cartItems) { var cartItemParts = cartItem.Split(':'); if (cartItemParts[0] == skuId) { newCartInfo += "," + skuId + ":" + (int.Parse(cartItemParts[1]) + count); exist = true; } else { newCartInfo += "," + cartItem; } } if (!exist) { newCartInfo += "," + skuId + ":" + count; } if (!string.IsNullOrWhiteSpace(newCartInfo)) { newCartInfo = newCartInfo.Substring(1); } WebHelper.SetCookie(CookieKeysCollection.HIMALL_CART, newCartInfo); } else { WebHelper.SetCookie(CookieKeysCollection.HIMALL_CART, string.Format("{0}:{1}", skuId, count)); } } }
public object PostUpdateCartItem(CartUpdateCartItemModel value) { var productService = ServiceProvider.Instance <IProductService> .Create; CheckUserLogin(); string Jsonstr = value.jsonstr; var datavalue = Newtonsoft.Json.JsonConvert.DeserializeObject <UpdateCartSKusModel>(Jsonstr); var cartService = ServiceProvider.Instance <ICartService> .Create; var ladderPrice = 0m; long productId = 0; var isOpenLadder = false; foreach (var sku in datavalue.skus) { Entities.SKUInfo skuinfo = OrderApplication.GetSkuByID(sku.skuId); if (skuinfo != null) { var productInfo = ProductManagerApplication.GetProduct(skuinfo.ProductId); if (productInfo != null) { if (productInfo.MaxBuyCount > 0 && sku.count > productInfo.MaxBuyCount && !productInfo.IsOpenLadder) { return(ErrorResult(string.Format("商品[{0}]限购{1}件", productInfo.ProductName, productInfo.MaxBuyCount), data: new { maxBuyCount = productInfo.MaxBuyCount })); //throw new MallException(string.Format("每个ID限购{0}件", productInfo.MaxBuyCount)); } } } cartService.UpdateCart(sku.skuId, sku.count, CurrentUser.Id); #region 阶梯价--张宇枫 var skus = CartApplication.GetCart(CurrentUser.Id); var skuItem = skus.Items.ToList().Find(i => i.SkuId == sku.skuId); productId = skuItem.ProductId; var product = ProductManagerApplication.GetProduct(productId); isOpenLadder = product.IsOpenLadder; if (isOpenLadder) { var shop = ShopApplication.GetShop(product.ShopId); var groupCartByProduct = skus.Items.Where(item => item.ShopBranchId == 0).Select(c => { var cItem = new Mall.Entities.ShoppingCartItem(); var skuInfo = productService.GetSku(c.SkuId); if (skuInfo != null) { cItem = c; } return(cItem); }).GroupBy(i => i.ProductId).ToList(); var quantity = groupCartByProduct.Where(i => i.Key == productId) .ToList() .Sum(cartitem => cartitem.Sum(i => i.Quantity)); ladderPrice = ProductManagerApplication.GetProductLadderPrice(skuItem.ProductId, quantity); if (shop.IsSelf) { ladderPrice = CurrentUser.MemberDiscount * ladderPrice; } } #endregion } var result = new { success = true, //d.Url = "http://" + Url.Request.RequestUri.Host + "/m-IOS/Order/SubmiteByCart"; Url = Core.MallIO.GetRomoteImagePath("/m-IOS/Order/SubmiteByCart"), Price = ladderPrice.ToString("F2"), ProductId = productId, IsOpenLadder = isOpenLadder ? 1 : 0, }; return(result); }