1
Vote

Bug (and fix) in ShoppingCart.aspx

description

The UpdateTotals() routine does not work if you have 2 or more items with the first item having more than 1 quantity in the shopping cart.
The problem is the cartItems enumerator is in the for loop so it gets reset back to the beginning of the enumeration for each item.
 
Also the int i = 0; and i += 1; in the routine serves no purpose and can be deleted.
 
The fix is remove the cartItems enumerator out of the for loop and paste it above the for loop instead. Below is the fixed code:
 
protected void UpdateTotals()
{
 
    IEnumerator cartItems = lShoppingCart.Items.GetEnumerator();
 
    foreach (ListViewDataItem lvdi in lvOrderItems.Items)
    {
 
        ShoppingCartItem lCartItem = null; // = (ShoppingCartItem)lShoppingCart.Items.ElementAtOrDefault(i);
 
        while (cartItems.MoveNext())
        {
            lCartItem = (ShoppingCartItem)cartItems.Current;
            break;
        }
 
        if (lCartItem != null)
        {
 
            int id = lCartItem.ID;
            int quantity = int.Parse(((TextBox)lvdi.FindControl("txtQuantity")).Text);
 
            lShoppingCart.UpdateItemQuantity(id, quantity);
        }
 
    }

comments

vanbach1304 wrote May 14, 2010 at 9:27 AM

You can fix this by adding code to commerce.js file for after delete item the page will reload.
$(".delete-item-button").click(function () {
var productId = $(this).attr("meta:id");
var form = $(this).parent("form");
$.post(
    "/Commerce/DeleteShoppingCartItem",
    { id: productId },
    function (data) {
        $("#item-" + data.object.id).remove();
        //Reload whole page
        location.reload(form);
    },
    "json"
);
return false;
});

sisnaz wrote Jun 3, 2010 at 11:13 PM

I posted the fix above - The cartItems enumeration should not be in the for loop.... additionally the problem isn't just with deleting items.

wrote Feb 21, 2013 at 11:12 PM