Adding multiple products with one click via Ajax request (already working but not efficient)

Adding multiple products with one click via Ajax request (already working but not efficient)

mrsilver123
Shopify Partner
17 0 1

So this is working for me, but ideally i would like to not iterate through the array recusrively like this, and just pass in the array, but I cant get that to work. Can anybody improve on the below for me? 

Thanks!

        function ajax_AddToCart(itemsToAdd, itemIndex){

            jQuery.ajax({
              type: 'POST',
              url: '/cart/add.js',
              data: itemsToAdd[itemIndex],
              dataType: 'json',
              success: function() {
                    itemIndex += 1;
                    if(itemIndex < numItems){
                      ajax_AddToCart(itemsToAdd, itemIndex)
                    } else{
                      location.href = "/cart"
                    }
              }
            });

        }



Replies 0 (0)