예제 #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
            });
        }