OrderSummary - Warnings get lost

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I am Tracking Quantity and when on the Single Product page, it displays an error if I try to ADD to CART a qty bigger than the current StockQuantity >> That's GOOD.

Now I move to the Cart - OrderSummary page
...and try to do the same.

The ShoppingCartController.UpdateCart method gets called and when it iterates thru the cart items, it calls

            foreach (var sci in cart)
            {
                   ....bla bla bla

                    _shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
                        sci.Id, newQuantity, true);
             }

it picks up the Warning (not even sure why the Error has become a Warning here).

Anyhow, now i have the Warning telling that the user is trying to buy more items than the ones in stock.
>> That's GOOD.


But then the Controller calls
            var model = PrepareShoppingCartModel(new ShoppingCartModel(), cart, true);

and pass the initial Qty amount, so this call does NOT generate the Warning, pretty much hiding the real Warning.
The view receives a Warnings.Count = 0 so nothing gets displayed and the user will never know WHY the requested Quantity was NOT applied.

=======================
Maybe we should hold the warnings from the foreach loop on a temp variable
and pass it to the model when it gets returned?


Just a thought....
12 years ago
And that's one way to solve this issue:

Presentation\Nop.Web\Controllers\ShoppingCartController.cs
==============================================================

        [ValidateInput(false)]
        [HttpPost, ActionName("Cart")]
        [FormValueRequired("updatecart")]
        public ActionResult UpdateCart(FormCollection form)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.EnableShoppingCart))
                return RedirectToAction("Index", "Home");

            var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();

            // << Added InnerWarning Var
            var innerWarnings = new List<string>();

            foreach (var sci in cart)
            {
                    int newQuantity = sci.Quantity;
                    foreach (string formKey in form.AllKeys)
                        if (formKey.Equals(string.Format("itemquantity{0}", sci.Id), StringComparison.InvariantCultureIgnoreCase))
                        {
                            int.TryParse(form[formKey], out newQuantity);
                            break;
                        }

                // << Now storing Current Warning
                var currWarning = _shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
                        sci.Id, newQuantity, true);

                // << Adding them to the InnerWarning Var
                foreach (string s in currWarning)
                    innerWarnings.Add(s);
            }

            //updated cart
            cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();
            var model = PrepareShoppingCartModel(new ShoppingCartModel(), cart, true);

            // << Adding them to the Model.Warning
            foreach (string s in innerWarnings)
                model.Warnings.Add(s);
            
            return View(model);
        }

====================================
Hope it helps ...
12 years ago
Thanks!

UPDATE:
BTW, the same fix should be applied to wishlist
12 years ago
Shall we assume that this (or another fix) will appear on the next release ?
12 years ago
Already fixed. Changeset ca73ee1b96c1
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.