Beispiel #1
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
            });
        }