Esempio n. 1
0
        public ActionResult AddToCart(int productId, int qty)
        {
            var customerId = AbpSession.UserId.HasValue ? Convert.ToInt32(AbpSession.UserId) : 0;

            if (customerId == 0)
            {
                return(AjaxResult(Framework.UIEnums.AjaxResultStatus.Error, "请在微信中操作"));
            }

            var product = _productService.GetProductById(productId);

            if (product == null)
            {
                return(AjaxResult(Framework.UIEnums.AjaxResultStatus.Error, "该商品不存在"));
            }

            var carts = _cartService.GetAllShoppingItems(customerId: customerId,
                                                         productId: productId);
            ShoppingCartItem cart = null;

            if (carts.TotalCount == 0)
            {
                cart = new ShoppingCartItem
                {
                    CustomerId                = customerId,
                    Price                     = product.Price,
                    ProductId                 = product.Id,
                    Quantity                  = qty,
                    SpecialPrice              = product.SpecialPrice,
                    SpecialPriceEndDateTime   = product.SpecialPriceEndDateTime,
                    SpecialPriceStartDateTime = product.SpecialPriceStartDateTime,
                    CreationTime              = DateTime.Now,
                    PreSell                   = product.PreSell,
                };
                _cartService.InsertCart(cart);
            }
            else
            {
                cart.Quantity     = cart.Quantity + qty;
                cart.CreationTime = DateTime.Now;
                _cartService.UpdateCart(cart);
            }
            return(AjaxResult(Framework.UIEnums.AjaxResultStatus.Success, "上仙!您的商品已经到了购物车了!"));
        }
        public void InsertCart(ShoppingCartItemModel cart)
        {
            var entity  = cart.MapTo <ShoppingCartItem>();
            var product = _productService.GetProductById(entity.ProductId);

            entity.SpecialPrice              = product.SpecialPrice;
            entity.SpecialPriceEndDateTime   = product.SpecialPriceEndDateTime;
            entity.SpecialPriceStartDateTime = product.SpecialPriceStartDateTime;
            entity.Price   = product.Price;
            entity.PreSell = product.PreSell;
            _cartService.InsertCart(entity);
        }