コード例 #1
0
        public ActionResult Remove(
            int?productVarientID = null,
            int?productID        = null,
            int?packageID        = null)
        {
            var jsonSuccessResult = new JsonSuccessResult();

            Cart cart;

            try
            {
                cart = CartController.GetCart(HttpContext);

                bool exists = false;
                if (CartItems.Exists(cart.ID, productVarientID, productID, packageID))
                {
                    CartItems.Delete(cart.ID, productVarientID, productID, packageID);

                    exists = true;
                }

                var cartItems = CartItems.GetByCartID(cart.ID, UserID);

                int total         = 0,
                    totalDiscount = 0;

                foreach (var item in cartItems)
                {
                    totalDiscount += item.Quantity * (item.DiscountPercent > 0 ? item.DiscountPrice : item.Price);
                    total         += item.Quantity * item.Price;
                }

                jsonSuccessResult.Data = new
                {
                    Exists        = exists,
                    CartItems     = cartItems,
                    Total         = total,
                    TotalDiscount = totalDiscount,
                };
                jsonSuccessResult.Success = true;
            }
            catch (DbException ex)
            {
                jsonSuccessResult.Errors  = ex.Errors.ToArray();
                jsonSuccessResult.Success = false;
            }
            catch (Exception ex)
            {
                jsonSuccessResult.Errors  = new string[] { ex.Message };
                jsonSuccessResult.Success = false;
            }

            return(new JsonResult()
            {
                Data = jsonSuccessResult
            });
        }
コード例 #2
0
 /// <summary>
 /// 判断指定的SKU商品是否存在于购物车中
 /// </summary>
 /// <param name="sku">货品编号(唯一)</param>
 /// <returns>TRUE:存在 FALSE:不存在</returns>
 public bool IsExistsItem(string sku)
 {
     return(CartItems.Exists(p => p.SKU == sku));
 }
コード例 #3
0
        public ActionResult Add(
            int?productVarientID = null,
            int?productID        = null,
            int?packageID        = null,
            byte quantity        = 1)
        {
            var jsonSuccessResult = new JsonSuccessResult();

            try
            {
                Cart cart = CartController.GetCart(HttpContext);

                bool exists = true;
                bool canAdd = true;

                if (!CartItems.Exists(cart.ID, productVarientID, productID, packageID))
                {
                    canAdd = Products.CanAddToCart(productVarientID, productID, packageID);

                    if (canAdd)
                    {
                        var cartItem = new CartItem();

                        cartItem.CartID = cart.ID;

                        cartItem.ProductVarientID = productVarientID;
                        cartItem.ProductID        = productID;
                        cartItem.PackageID        = packageID;

                        cartItem.DateTime = DateTime.Now;
                        cartItem.Quantity = quantity;
                        cartItem.Price    = -1;

                        CartItems.Insert(cartItem);
                    }

                    exists = false;
                }

                var cartItems = CartItems.GetByCartID(cart.ID, UserID);

                int total         = 0,
                    totalDiscount = 0;

                foreach (var item in cartItems)
                {
                    totalDiscount += item.Quantity * (item.DiscountPercent > 0 ? item.DiscountPrice : item.Price);
                    total         += item.Quantity * item.Price;
                }

                jsonSuccessResult.Data = new
                {
                    Exists        = exists,
                    CanAdd        = canAdd,
                    CartItems     = cartItems,
                    Total         = total,
                    TotalDiscount = totalDiscount,
                };
                jsonSuccessResult.Success = true;
            }
            catch (DbException ex)
            {
                jsonSuccessResult.Errors  = ex.Errors.ToArray();
                jsonSuccessResult.Success = false;
            }
            catch (Exception ex)
            {
                jsonSuccessResult.Errors  = new string[] { ex.Message };
                jsonSuccessResult.Success = false;
            }

            return(new JsonResult()
            {
                Data = jsonSuccessResult
            });
        }