Exemple #1
0
        public CSCartBaseJsonResult GetCurrentCart()
        {
            var id   = this.CurrentVisitorContext.GetCustomerId();
            var cart = _cartCacheService.GetCart(id);

            CSCartBaseJsonResult cartResult;

            // The following condition stops the creation of empty carts on startup.
            if (cart == null && CartCookieHelper.DoesCookieExistForCustomer(id))
            {
                var response = _cartManager.GetCurrentCart(CurrentStorefront, CurrentVisitorContext, true);
                cartResult = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    cartResult.Initialize(response.ServiceProviderResult.Cart, _productResolver);
                    if (response.ServiceProviderResult.Cart != null)
                    {
                        _cartCacheService.AddToCart(response.ServiceProviderResult.Cart as CommerceCart);
                    }
                }
            }
            else
            {
                cartResult = new CSCartBaseJsonResult();
                cartResult.Initialize(cart, _productResolver);
            }

            return(cartResult);
        }
        public JsonResult SetPaymentMethods(PaymentInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.CartManager.SetPaymentMethods(this.CurrentStorefront, this.CurrentVisitorContext, inputModel);
                var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (!response.ServiceProviderResult.Success || response.Result == null)
                {
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }

                result.Initialize(response.Result);

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("SetPaymentMethods", this, e);
                return(Json(new BaseJsonResult("SetPaymentMethods", e), JsonRequestBehavior.AllowGet));
            }
        }
Exemple #3
0
        public JsonResult RemoveDiscount(DiscountInputModel model)
        {
            try
            {
                Assert.ArgumentNotNull(model, "model");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.CartManager.RemovePromoCodeFromCart(CurrentStorefront, CurrentVisitorContext, model.PromoCode);
                var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("RemoveDiscount", this, e);
                return(Json(new BaseJsonResult("RemoveDiscount", e), JsonRequestBehavior.AllowGet));
            }
        }
Exemple #4
0
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.CartManager.ChangeLineQuantity(CurrentStorefront, CurrentVisitorContext, inputModel);
                var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("UpdateLineItem", this, e);
                return(Json(new BaseJsonResult("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }
Exemple #5
0
        public CSCartBaseJsonResult SetShippingMethods(SetShippingMethodsInputModel inputModel)
        {
            var response = this._cartManager.SetShippingMethods(CurrentStorefront, CurrentVisitorContext, inputModel);
            var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                result.Initialize(response.Result, _productResolver);
            }

            return(result);
        }
Exemple #6
0
        public CSCartBaseJsonResult DeleteLineItem(DeleteCartLineInputModel model)
        {
            var response = _cartManager.RemoveLineItemFromCart(CurrentStorefront, CurrentVisitorContext, model.ExternalCartLineId);
            var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                result.Initialize(response.Result, _productResolver);
            }

            return(result);
        }
Exemple #7
0
        public CSCartBaseJsonResult ApplyDiscount(DiscountInputModel model)
        {
            var response = _cartManager.AddPromoCodeToCart(CurrentStorefront, CurrentVisitorContext, model.PromoCode);
            var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                result.Initialize(response.Result, _productResolver);
            }

            return(result);
        }
Exemple #8
0
        public CSCartBaseJsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            var response = _cartManager.ChangeLineQuantity(CurrentStorefront, CurrentVisitorContext, inputModel);
            var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);

            if (response.ServiceProviderResult.Success && response.Result != null)
            {
                result.Initialize(response.Result, _productResolver);

                if (response.Result.HasBasketErrors())
                {
                    // We clear the cart from the cache when basket errors are detected.  This stops the message from being displayed over and over as the
                    // cart will be retrieved again from CS and the pipelines will be executed.
                    _cartCacheService.Invalidate(CurrentVisitorContext.GetCustomerId());
                }
            }

            return(result);
        }
Exemple #9
0
        public JsonResult GetCurrentCart()
        {
            try
            {
                var response = this.CartManager.GetCurrentCart(CurrentStorefront, CurrentVisitorContext, false);
                CSCartBaseJsonResult cartResult = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    cartResult.Initialize(response.ServiceProviderResult.Cart);
                }

                return(Json(cartResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("GetCurrentCart", this, e);
                return(Json(new BaseJsonResult("GetCurrentCart", e), JsonRequestBehavior.AllowGet));
            }
        }
Exemple #10
0
        public JsonResult GetCurrentCart()
        {
            try
            {
                var id        = this.CurrentVisitorContext.GetCustomerId();
                var cartCache = CommerceTypeLoader.CreateInstance <CartCacheHelper>();
                var cart      = cartCache.GetCart(id);
                CSCartBaseJsonResult cartResult;

                // The following condition stops the creation of empty carts on startup.
                if (cart == null && CartCookieHelper.DoesCookieExistForCustomer(id))
                {
                    var response = this.CartManager.GetCurrentCart(CurrentStorefront, CurrentVisitorContext, true);
                    cartResult = new CSCartBaseJsonResult(response.ServiceProviderResult);
                    if (response.ServiceProviderResult.Success && response.Result != null)
                    {
                        cartResult.Initialize(response.ServiceProviderResult.Cart);
                        if (response.ServiceProviderResult.Cart != null)
                        {
                            cartCache.AddCartToCache(response.ServiceProviderResult.Cart as CommerceCart);
                        }
                    }
                }
                else
                {
                    cartResult = new CSCartBaseJsonResult();
                    cartResult.Initialize(cart);
                }

                return(Json(cartResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("GetCurrentCart", this, e);
                return(Json(new BaseJsonResult("GetCurrentCart", e), JsonRequestBehavior.AllowGet));
            }
        }
Exemple #11
0
        public JsonResult UpdateLineItem(UpdateCartLineInputModel inputModel)
        {
            try
            {
                Assert.ArgumentNotNull(inputModel, "inputModel");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var response = this.CartManager.ChangeLineQuantity(CurrentStorefront, CurrentVisitorContext, inputModel);
                var result   = new CSCartBaseJsonResult(response.ServiceProviderResult);
                if (response.ServiceProviderResult.Success && response.Result != null)
                {
                    result.Initialize(response.Result);

                    if (response.Result.HasBasketErrors())
                    {
                        // We clear the cart from the cache when basket errors are detected.  This stops the message from being displayed over and over as the
                        // cart will be retrieved again from CS and the pipelines will be executed.
                        var cartCache = CommerceTypeLoader.CreateInstance <CartCacheHelper>();
                        cartCache.InvalidateCartCache(this.CurrentVisitorContext.GetCustomerId());
                    }
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("UpdateLineItem", this, e);
                return(Json(new BaseJsonResult("UpdateLineItem", e), JsonRequestBehavior.AllowGet));
            }
        }