Пример #1
0
        public static AddToCartContext ConvertToAddToCartContext(this AddToCartPostModel addToCartPostModel, Customer customer, CartTypeEnum cartType)
        {
            return(new AddToCartContext()
            {
                Customer = customer,
                CartType = cartType,

                ShoppingCartRecId = addToCartPostModel.CartRecordId,
                Quantity = addToCartPostModel.Quantity,

                ProductId = addToCartPostModel.ProductId,
                VariantId = addToCartPostModel.VariantId,
                UpsellProducts = addToCartPostModel.UpsellProducts,
                IsWishlist = addToCartPostModel.IsWishlist,

                CustomerEnteredPrice = addToCartPostModel.CustomerEnteredPrice,
                Color = addToCartPostModel.Color,
                Size = addToCartPostModel.Size,
                TextOption = addToCartPostModel.TextOption,

                TemporaryImageNameStub = string.Empty,
                KitData = null,
                Composition = null
            });
        }
Пример #2
0
        public ActionResult AddToCart(
            int productId                = 0,
            int variantId                = 0,
            int?cartRecordId             = null,
            int quantity                 = 1,
            decimal customerEnteredPrice = 0,
            string color                 = "",
            string size           = "",
            string textOption     = "",
            string upsellProducts = "",
            bool isWishlist       = false,
            string returnUrl      = "")
        {
            if (variantId == 0)
            {
                variantId = AppLogic.GetDefaultProductVariant(productId);
            }

            if (productId == 0)
            {
                productId = AppLogic.GetVariantProductID(variantId);
            }

            if (productId == 0 || variantId == 0)
            {
                throw new Exception("Make sure to specify a product or variant id");
            }

            var customer = HttpContext.GetCustomer();
            var variant  = new ProductVariant(variantId);
            var product  = new Product(productId);

            var model = new AddToCartPostModel
            {
                ProductId            = productId,
                VariantId            = variantId,
                CartRecordId         = cartRecordId,
                Quantity             = quantity,
                CustomerEnteredPrice = customerEnteredPrice,
                Color          = color,
                Size           = size,
                TextOption     = textOption,
                UpsellProducts = upsellProducts,
                IsWishlist     = isWishlist,
                ReturnUrl      = returnUrl
            };

            return(AddToCart(model));
        }
Пример #3
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 }));
        }
Пример #4
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));
        }