Example #1
0
        public ActionResult AddToCart(AddToCartPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(ActionNames.Index, ControllerNames.Minicart));
            }

            var customer = HttpContext.GetCustomer();
            var notices  = new List <Notice>();

            var cartType = model.IsWishlist
                                ? CartTypeEnum.WishCart
                                : CartTypeEnum.ShoppingCart;

            // Use the provided return URL if there is one, otherwise try to use the referrer
            string unverifiedReturnUrl;

            if (!string.IsNullOrWhiteSpace(model.ReturnUrl))
            {
                unverifiedReturnUrl = model.ReturnUrl;
            }
            else if (Request.UrlReferrer != null)
            {
                unverifiedReturnUrl = Request.UrlReferrer.ToString();
            }
            else
            {
                unverifiedReturnUrl = null;
            }

            // Ensure we have a safe return URL
            var returnUrl = UrlHelper.MakeSafeReturnUrl(
                returnUrl: unverifiedReturnUrl,
                defaultUrl: cartType == CartTypeEnum.WishCart
                                        ? Url.Action(ActionNames.Index, ControllerNames.Wishlist)
                                        : Url.Action(ActionNames.Index, ControllerNames.Checkout));

            // Make request, check status
            var response = CartActionProvider.AddItemToCart(model.ConvertToAddToCartContext(
                                                                customer,
                                                                model.IsWishlist
                                        ? CartTypeEnum.WishCart
                                        : CartTypeEnum.ShoppingCart));

            switch (response.Status)
            {
            case CartActionStatus.Forbidden:
                throw new HttpException(403, "Forbidden");

            case CartActionStatus.RequiresLogin:
            case CartActionStatus.SessionTimeout:
            {
                foreach (var message in response.Messages)
                {
                    NoticeProvider.PushNotice(message.ConvertToNotice());
                }
                return(RedirectToAction(ActionNames.SignIn, ControllerNames.Account));
            }

            case CartActionStatus.ValidationErrors:
            {
                foreach (var message in response.Messages)
                {
                    NoticeProvider.PushNotice(message.ConvertToNotice());
                }
                return(Redirect(returnUrl));
            }

            default:
                break;
            }

            // Add any returned notices
            if (response.Messages.Any())
            {
                foreach (var message in response.Messages)
                {
                    NoticeProvider.PushNotice(message.ConvertToNotice());
                }
            }
            else
            {
                NoticeProvider.PushNotice("Item successfully added to cart.", NoticeType.Success);
            }

            // Redirect accordingly
            if (AppLogic.AppConfig("AddToCartAction").Equals("STAY", StringComparison.OrdinalIgnoreCase))
            {
                return(Redirect(returnUrl));
            }

            return(RedirectToAction(
                       ActionNames.Index,
                       ControllerNames.Checkout,
                       new { returnUrl }));
        }
Example #2
0
        public ActionResult AjaxAddToCart(AddToCartPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction(ActionNames.Index, ControllerNames.Minicart));
            }

            var customer = HttpContext.GetCustomer();
            var cartType = model.IsWishlist
                                ? CartTypeEnum.WishCart
                                : CartTypeEnum.ShoppingCart;

            // Make request, check status
            var response = CartActionProvider.AddItemToCart(
                model.ConvertToAddToCartContext(
                    customer,
                    cartType));

            switch (response.Status)
            {
            case CartActionStatus.Forbidden:
            case CartActionStatus.RequiresLogin:
            case CartActionStatus.SessionTimeout:
                if (response.Messages.Any())
                {
                    foreach (var message in response.Messages)
                    {
                        NoticeProvider.PushNotice(message.ConvertToNotice());
                    }
                }
                return(Json(
                           data: new AjaxAddToCartData(
                               status: response.Status.ToString(),
                               messages: null,
                               minicartData: GetMinicartData(customer, response.UpdatedCart, null)),
                           behavior: JsonRequestBehavior.AllowGet));

            default:
                break;
            }

            // Handle non-ajax calls
            if (!Request.IsAjaxRequest())
            {
                foreach (var message in response.Messages)
                {
                    NoticeProvider.PushNotice(message.ConvertToNotice());
                }
                return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
            }

            ModelState.Clear();

            // The view should reflect the real cart not the posted values in the modelstate.
            var notices = response.Messages
                          .Select(n => n.ConvertToAjaxNotice());
            // Pull cart now (to reflect any possible changes)
            var cart = response.UpdatedCart;

            return(Json(
                       data: new AjaxAddToCartData(
                           status: response.Status.ToString(),
                           messages: notices,
                           minicartData: GetMinicartData(customer, cart, notices)),
                       behavior: JsonRequestBehavior.AllowGet));
        }