コード例 #1
0
        public ActionResult AddCoupon()
        {
            var code = Request["couponcode"] ?? string.Empty;

            CurrentCart.AddCouponCode(code.Trim());
            HccApp.CalculateOrderAndSave(CurrentCart);
            return(Redirect(Url.RouteHccUrl(HccRoute.Cart)));
        }
コード例 #2
0
        private RedirectResult HandleActionParams()
        {
            //1. AddSKU - this is the sku that should be added to the cart automatically
            //2. AddSKUQTY - this is the quantity of the provided sku that should be added.
            //3. CouponCode - the coupon that should automatically be applied, if valid.
            //4. RedirectToCheckout=True - redirects the user automatically to the checkout.
            if (ModuleContext == null || ModuleContext.Configuration.DesktopModule.ModuleName != "Hotcakes.Cart")
            {
                return(null);
            }

            var clearCart          = (string)RouteData.Values["ClearCart"];
            var addSKU             = (string)RouteData.Values["AddSKU"];
            var addSKUQTY          = (string)RouteData.Values["AddSKUQTY"];
            var CouponCode         = (string)RouteData.Values["CouponCode"];
            var RedirectToCheckout = (string)RouteData.Values["RedirectToCheckout"];

            if (!string.IsNullOrWhiteSpace(addSKU) ||
                !string.IsNullOrWhiteSpace(addSKUQTY) ||
                !string.IsNullOrWhiteSpace(CouponCode))
            {
                HccApp.OrderServices.EnsureShoppingCart();
            }

            if (!string.IsNullOrWhiteSpace(clearCart))
            {
                var clear = bool.Parse(clearCart);
                if (clear)
                {
                    HccApp.ClearOrder(CurrentCart);
                }
            }

            string[] SKUs    = null;
            string[] SKUQTYs = null;
            if (!string.IsNullOrWhiteSpace(addSKU))
            {
                SKUs = addSKU.Split(',');
            }
            if (!string.IsNullOrWhiteSpace(addSKUQTY))
            {
                SKUQTYs = addSKUQTY.Split(',');
            }
            if (addSKU == null)
            {
                return(null);
            }
            if (SKUQTYs == null)
            {
                SKUQTYs = new string[SKUs.Length];
            }
            if (SKUQTYs != null && SKUs.Length != SKUQTYs.Length)
            {
                FlashFailure(Localization.GetString("ParamsCountMismatch"));
                return(null);
            }

            var errorsPresent = false;

            for (var i = 0; i < SKUs.Length; i++)
            {
                var sku    = SKUs[i];
                var skuQty = SKUQTYs[i];
                var qty    = 1;
                if (!string.IsNullOrEmpty(skuQty))
                {
                    if (!int.TryParse(skuQty, out qty))
                    {
                        errorsPresent = true;
                        FlashFailure(Localization.GetFormattedString("QuantityParsingError", skuQty));
                    }
                }

                var p = HccApp.CatalogServices.Products.FindBySku(sku);
                if (p != null)
                {
                    // TODO: check for products with options
                    AddSingleProduct(p, qty);
                }
                else
                {
                    errorsPresent = true;
                    FlashFailure(Localization.GetFormattedString("SKUNotExists", sku));
                }
            }

            if (!string.IsNullOrWhiteSpace(CouponCode))
            {
                CurrentCart.AddCouponCode(CouponCode.Trim());
                HccApp.CalculateOrderAndSave(CurrentCart);
            }

            if (!errorsPresent)
            {
                if (!string.IsNullOrWhiteSpace(RedirectToCheckout))
                {
                    var redirect = bool.Parse(RedirectToCheckout);
                    if (redirect)
                    {
                        Response.Redirect(Url.RouteHccUrl(HccRoute.Checkout));
                    }
                }
                return(Redirect(Url.RouteHccUrl(HccRoute.Cart)));
            }
            return(null);
        }