Exemplo n.º 1
0
        public ActionResult UpdateBundleQty(string id, int?quantity)
        {
            if (!CheckAccess())
            {
                return(BounceToLogin());
            }
            int newQuantity = 1;

            if (quantity.HasValue && quantity.Value > 0 && quantity.Value < 10000)
            {
                newQuantity = quantity.Value;
            }
            if (string.IsNullOrWhiteSpace(id))
            {
                AddUserMessage("Update Quantity Error", "Item '' not found. Please try again.", UserMessageType.Danger);
                return(RedirectToAction("Index"));
            }

            StoreFront storeFront = CurrentStoreFrontOrThrow;

            ProductBundle productBundle = storeFront.ProductBundles.AsQueryable().CanAddToCart(storeFront).SingleOrDefault(p => p.UrlName.ToLower() == id.ToLower());

            if (productBundle == null)
            {
                AddUserMessage("Update Quantity Error", "Bundle '" + id.ToHtml() + "' cannot be added to your cart. Please try again.", UserMessageType.Danger);
                return(RedirectToAction("Index"));
            }

            Cart cart = CurrentStoreFrontOrThrow.GetCart(Session.SessionID, CurrentUserProfileOrNull);

            CartBundle cartBundleExisting = cart.FindBundleInCart(productBundle);

            if (cartBundleExisting == null)
            {
                AddUserMessage("Update Quantity Error", "Bundle '" + id.ToHtml() + "' is not in your cart. Please try again.", UserMessageType.Success);
                return(RedirectToAction("Index"));
            }

            cartBundleExisting.UpdateQuantityAndSave(GStoreDb, newQuantity, this);
            cart.CancelCheckout(GStoreDb);

            return(RedirectToPreviousPageOrCartIndex());
        }
Exemplo n.º 2
0
        public ActionResult AddBundle(string id, int?qty, bool?buyNow)
        {
            //remove old item and add new item
            if (!CheckAccess())
            {
                return(BounceToLogin());
            }

            int quantity = 1;

            if (qty.HasValue && qty.Value > 0 && qty.Value < 10000)
            {
                quantity = qty.Value;
            }

            StoreFront storeFront = CurrentStoreFrontOrThrow;
            Cart       cart       = storeFront.GetCart(Session.SessionID, CurrentUserProfileOrNull);

            if (string.IsNullOrWhiteSpace(id))
            {
                AddUserMessage("Add to Cart Error", "Item not found. Please try again.", UserMessageType.Danger);
                GStoreDb.LogUserActionEvent(HttpContext, RouteData, this, UserActionCategoryEnum.Cart, UserActionActionEnum.Cart_AddToCartFailure, "Bad Url", false, cartId: (cart == null ? (int?)null : cart.CartId), productBundleUrlName: id);
                return(RedirectToAction("Index"));
            }

            ProductBundle productBundle = storeFront.ProductBundles.AsQueryable().CanAddToCart(storeFront).SingleOrDefault(p => p.UrlName.ToLower() == id.ToLower());

            if (productBundle == null)
            {
                AddUserMessage("Add to Cart Error", "Item '" + id.ToHtml() + "' could not be found to add to your cart. Please try again.", UserMessageType.Danger);
                GStoreDb.LogUserActionEvent(HttpContext, RouteData, this, UserActionCategoryEnum.Cart, UserActionActionEnum.Cart_AddToCartFailure, "Item Not Found", false, cartId: (cart == null ? (int?)null : cart.CartId), productBundleUrlName: id);
                return(RedirectToPreviousPageOrCartIndex());
            }

            if (!productBundle.AvailableForPurchase)
            {
                AddUserMessage("Add to Cart Error", "Item '" + id.ToHtml() + "' is not available for purchase online. Please try again.", UserMessageType.Danger);
                GStoreDb.LogUserActionEvent(HttpContext, RouteData, this, UserActionCategoryEnum.Cart, UserActionActionEnum.Cart_AddToCartFailure, "Product not available for purchase", false, cartId: (cart == null ? (int?)null : cart.CartId), productBundleUrlName: id);
                return(RedirectToPreviousPageOrCartIndex());
            }

            //if item with same variant is already added, increment the quantity
            if (!CurrentStoreFrontConfigOrThrow.UseShoppingCart)
            {
                if (cart != null && cart.CartItems.Count > 0)
                {
                    //if storefront is not set to use a cart, dump previous items and start with a new cart.
                    CurrentStoreFrontOrThrow.DumpCartNoSave(GStoreDb, cart);
                    GStoreDb.SaveChanges();
                    cart = storeFront.GetCart(Session.SessionID, CurrentUserProfileOrNull);
                }
            }

            CartBundle cartBundleExisting = cart.FindBundleInCart(productBundle);

            if (cartBundleExisting != null)
            {
                int newQty = cartBundleExisting.Quantity + quantity;
                cartBundleExisting = cartBundleExisting.UpdateQuantityAndSave(GStoreDb, newQty, this);

                if (newQty <= cartBundleExisting.ProductBundle.MaxQuantityPerOrder)
                {
                    AddUserMessage("Bundle Added to Cart", "'" + cartBundleExisting.ProductBundle.Name.ToHtml() + "' was added to your cart. Now you have " + cartBundleExisting.Quantity + " of them in your cart.<br/><a href=" + Url.Action("Index", "Cart") + ">Click here to view your cart.</a>", UserMessageType.Success);
                    cart.CancelCheckout(GStoreDb);
                }
                else
                {
                    //quantity is over max, user messages are already set
                }
                GStoreDb.LogUserActionEvent(HttpContext, RouteData, this, UserActionCategoryEnum.Cart, UserActionActionEnum.Cart_AddToCartSuccess, "Added to Existing", true, cartId: cart.CartId, productBundleUrlName: id);

                if (buyNow ?? false)
                {
                    return(RedirectToAction("Index", "Checkout"));
                }
                return(RedirectToPreviousPageOrCartIndex());
            }

            CartBundle cartBundle = cart.AddBundleToCart(productBundle, quantity, this);

            AddUserMessage("Bundle Added to Cart", "'" + cartBundle.ProductBundle.Name.ToHtml() + "' is now in your shopping cart.<br/><a href=" + Url.Action("Index", "Cart") + ">Click here to view your cart.</a>", UserMessageType.Success);

            GStoreDb.LogUserActionEvent(HttpContext, RouteData, this, UserActionCategoryEnum.Cart, UserActionActionEnum.Cart_AddToCartSuccess, "Added", true, cartId: cartBundle.CartId, productBundleUrlName: id);

            cart.CancelCheckout(GStoreDb);

            if (buyNow.HasValue && buyNow.Value)
            {
                return(RedirectToAction("Index", "Checkout"));
            }

            return(RedirectToPreviousPageOrCartIndex());
        }