bleroy / Nwazet.Commerce

Idiomatic commerce module for Orchard CMS.
BSD 3-Clause "New" or "Revised" License
26 stars 21 forks source link

Shopping Cart Widget when Product does not exist #22

Closed bleroy closed 8 years ago

bleroy commented 10 years ago

Originally reported by: Daniel Higgins (Bitbucket: daniel23, GitHub: daniel23)


Hi Bertrand,

After recooking orchard I noticed that if there is an item in the localStorage cart (client-side javascript) that does not correspond to a product in the database (by product Id), the shopping cart widget fails to load properly when the javascript tries to load the shopping cart. If the cart it gets from the server is empty (NakedCart action, I believe), it will try to load the cart from localStorage. Here it picks up the offending item. The script onCartLoad will then post this to the AjaxUpdate action and pass it the offending item. AjaxUpdate will return a server error, and the cart widget fails to load properly.

You can reproduce this by running the following line in the java script. This will create the phantom item.

    localStorage["nwazet.cart"] = '{ "Country": "United States", "ZipCode": "02459", "items[0].ProductId": "6660666", "items[0].Quantity": "1" }';

After than, any page with the cart widget will get a server failure error from the AjaxUpdate action. The ShoppingCart.GetProducts throws a "Sequence contains no matching element" exception with the following call stack:

#!c#

nwazet.commerce.dll!Nwazet.Commerce.Models.ShoppingCart.GetProducts() Line 119 + 0x9a bytes C#

nwazet.commerce.dll!Nwazet.Commerce.Controllers.ShoppingCartController.BuildCartShape(bool isSummary = true, string country = "United States", string zipCode = "02459", Nwazet.Commerce.Models.ShippingOption shippingOption = null) Line 103 + 0x58 bytes C#
    nwazet.commerce.dll!Nwazet.Commerce.Controllers.ShoppingCartController.AjaxUpdate(Nwazet.Commerce.ViewModels.UpdateShoppingCartItemViewModel[] items = {Nwazet.Commerce.ViewModels.UpdateShoppingCartItemViewModel[1]}, string country = "United States", string zipCode = "02459") Line 206 + 0x26c bytes  C#

bleroy commented 10 years ago

Original comment by Bertrand Le Roy (Bitbucket: bleroy, GitHub: bleroy):


Good catch, thanks.