예제 #1
0
        private ActionResult JsAddItemToCart(Models.Product product, int quantity)
        {
            if (product == null)
            {
                return new JsonResult();
            }
            quantity = GetQuantity(product, quantity);
            bool isCustomerPriceApplied = false;
            var price = SalesService.GetProductSalePrice(product, User.GetUserPrincipal().CurrentUser, quantity, out isCustomerPriceApplied);
            var cart = CartService.GetOrCreateQuoteCart(User.GetUserPrincipal());

            if (this.Request.UrlReferrer != null)
            {
                cart.LastPage = this.Request.UrlReferrer.PathAndQuery;
            }
            else
            {
                cart.LastPage = "/";
            }

            using (var ts = TransactionHelper.GetNewReadCommitted())
            {
                CartService.AddItem(cart, product, quantity, price, isCustomerPriceApplied);
                CartService.Save(cart);
                ts.Complete();
            }

            var urlHelper = new UrlHelper(this.ControllerContext.RequestContext);

            return Json(new
            {
                title = product.Title,
                quantity = quantity,
                cartUrl = urlHelper.QuoteCartHref(),
                productImage = product.DefaultImage != null ? HttpUtility.UrlEncode(product.DefaultImage.Url) : string.Empty,
            });
        }