How to remove multiple items from the cart at the same time?

Highlighted
Shopify Partner
4 0 0

Hi,

 

Is it possible to remove multiple items from the cart at the same time? Here's the code I have but I keep receiving 400 errors from cart/change.js. Can someone please help?

 

function removeItems(items) {
  var itemsProcessed = items.slice(0,-1);
  var itemsArray = itemsProcessed.split(',');
  console.log(itemsArray); // ["32216279777361", "32216279810129", "32216279711825", "32216279744593", "32216279842897"]

  // jQuery.ajax({
  //   type: 'post',
  //   url: '/cart/change.js',
  //   data: data,
  //   success: function(d){
  //     console.log(d)
  //   },
  //   dataType: 'json'
  // });
}
0 Likes
Highlighted
Shopify Partner
4 0 0

Just figured out a method that works fairly well so thought I'd update this:

 

function removeItems(items) {
  var itemsProcessed = items.slice(0,-1);
  var itemsArray = itemsProcessed.split(',');
  var qty = 0;
  var data = { updates: {} };
 
  for (i = 0; i < itemsArray.length; i++) {
     data.updates[itemsArray[i]] = qty;
  }

  jQuery.ajax({
    type: 'POST',
    url: '/cart/update.js',
    data: data,
    dataType: 'json',
    success: function() { 
      alert('success!');
    }
  });

}
0 Likes
Highlighted
Shopify Expert
130 0 27

Thanks for this stevejcoates! can you explain how the script you posted is targeting the items to be removed? If I want to remove all items with a certain data attribute, is that possible? Any help would be GREATLY appreciated :)

0 Likes