tags:

views:

91

answers:

3

Hi, I've been at this bit of code for a while, but am not sure what I'm doing wrong. Basically I want to create a loop that prints out all items in a cart. If I take the loop out, it prints the item just added, but when I add the loop in, it breaks.

I'm not too familiar with jQuery/JSAON, so if anyone could point out where I might be going wrong, that would be great.

(edit - full js files)

var ShoppCartAjaxHandler = function (cart) {
    (function($) {
     var display = $('#shopp-cart-ajax');
     display.empty().hide(); // clear any previous additions
     var item = $('<ul class="sidecart_list"></ul>').appendTo(display);
     $.each(cart.items, function() {
      $("<li></li>").html("<strong>"+this.quantity+"</strong>"+this.name).appendTo(item);
     });
     //$('<li></li>').html('<strong>'+cart.Item.quantity+'</strong> x '+cart.Item.name).appendTo(item);
     $('<li></li>').html('<strong>Subotal</strong> '+asMoney(cart.Totals.subtotal)).appendTo(item);
     $('<li></li>').html('<strong>Shipping</strong> '+asMoney(cart.Totals.shipping)).appendTo(item);
     $('<li></li>').html('<strong>Total</strong> '+asMoney(cart.Totals.total)).appendTo(item);
     if ($('#shopp-cart-items').length > 0) {
      $('#shopp-cart-items').html(cart.Totals.quantity);
      $('#shopp-cart-total').html(asMoney(cart.Totals.total));   
     } else {

      $('.widget_shoppcartwidget p.status').html('<p class="status_info"><strong><span id="shopp-cart-items">'+cart.Totals.quantity+'</span></strong> x <span id="shopp-cart-total">'+cart.Item.name+'</span></p><p class="status_info"><strong>Subtotal</strong> <span id="shopp-cart-subtotal">'+asMoney(cart.Totals.subtotal)+'</span></p><p class="status_info"><strong>Shipping</strong> <span id="shopp-cart-shipping">'+asMoney(cart.Totals.shipping)+'</span></p><p class="status_info"><strong>Total</strong> <span id="shopp-cart-total">'+asMoney(cart.Totals.total)+'</span></p>');

     }
     display.slideDown();
    })(jQuery) 
}
+1  A: 

Please change your call to $.each as follows:

$.each(
        cart.items, 
        function()
        {
          $("<li></li>").html("<strong>" + this.Quantity + "</strong>" +
                              this.name).appendTo(item);
        }
      );
David Andres
Thanks for your answer. The loop seems to be working now (it not defaulting to the inbuilt js file) but I'm not getting any output. I changed 'cart.items' to 'cart.Item' and got a whole lot of 'undefined' printed out, but not much else. You've removed the 'cart' from the string that is printed out. doesn't the script still need to refer to the cart?
Aaron Moodie
Aaron, I assumed you were trying to include the current item's qualities into the list item. The this keyword within the callback function always refers the current item (which is one of the items in the cart.items collection).
David Andres
The loop wasn't working as the cart items are accessed via cart.Contents. All working now
Aaron Moodie
+1  A: 

The following should work. Check out how each is called, and "this" refers to the items being iterated on. I also changed the variable "ul" to represent the list, so that it is clearer what is going on. Finally, make sure your cart looks like this:

var cart = { 
    items: [
     {quantity: 1, name: 'asjdfkj' },
     {quantity: 1, name: 'asjdfkj' },
     {quantity: 1, name: 'bkag' }
      ],
    Totals: { 
     subtotal: 12.95, 
     shipping: 2.34, 
     total: 15.00
    }

};

var asMoney=function(s) { return s; }


    var display = $('#shopp-cart-ajax');
    display.empty(); // clear any previous additions
    var ul = $('<ul class="sidecart_list"></ul>').appendTo(display);

    $.each(cart.items, function() {
      $('<li></li>').html('<strong>'+this.quantity+'</strong> x '+this.name).appendTo(ul);
    });
 $('<li></li>').html('<strong>Subotal</strong>'+asMoney(cart.Totals.subtotal)).appendTo(ul);
 $('<li></li>').html('<strong>Shipping</strong>'+asMoney(cart.Totals.shipping)).appendTo(ul);
    $('<li></li>').html('<strong>Total</strong> '+asMoney(cart.Totals.total)).appendTo(ul);
ndp
thanks ndp. the subtotals/shipping/totals are actually already printing out. It's just that it's only showing the items just added, instead of all items in the carts. so say I had:2 x item a1 x item bSubtotal $xxShipping $yyTotal $zzafter adding another item a to the cart, the cart refreshes, but only shows:3 x item aSubtotal $xxShipping $yyTotal $zz
Aaron Moodie
Aaron, are you sure your cart is properly populated and contains all items instead of only the most recently added?
David Andres
Hi David, I think this is where I'm going wrong. I wasn't sure how to update the <ul> containing already added elements, so thought I could jsut replace it when a new item was added ... I've updated the script at the top to show the whole js file, and where I'm at.
Aaron Moodie
Aaron, if you want to keep a persistent UL, you can grab it like so: var ul = $("UL.sidecart_lis"). That's one approach. though I do feel you'll have an easier time with maintaining a cart across adjustments to it (deletions are tricky). Perhaps you can store the cart JavaScript object globally (e.g., window["cart"]) and keep it maintained as changes are made to it.
David Andres
Thanks David. I'm not too worried about keeping a persistunt UL, so replacing it with another is fine really. The script currently updates a div #shopp-cart-ajax, and replaces ul#shopp-cart-items with ul.sidecart_list. The only thing that isn't working is that I can't get a print out of all the cart elements. If I can print out the item just added to the cart, then I should be able to print out all items in the cart? shouldn't it? I feel like it's almost there, but for some reason, the loop is breaking the script.
Aaron Moodie
If you alert cart.items.length prior to the loop, what quantity is shown?
David Andres
Hi David, didn't see your last comment. I'm still going on this. I've added in console.debug(cart) at the end of the script and am getting a "cannot access optimized closure" error on $.each(cart.items, function()
Aaron Moodie
A: 

It looks like this line is your problem:

var item = $('<ul class="sidecart_list"></ul>').appendTo(display);

You are appending all your items to an object that was only initially appended to your display. It might be best to just append all following items straight to the display or append the item object to the display after it's been populated.

fudgey