How to clear the cart using ajax in shopify?

Member

by jasen , in category: PHP CMS , 4 months ago

How to clear the cart using ajax in shopify?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , 4 months ago

@jasen 

To clear the cart using AJAX in Shopify, you can send a request to the cart endpoint with an empty cart object. Here's an example code snippet in JavaScript:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$.ajax({
  type: 'POST',
  url: '/cart/clear.js',
  dataType: 'json',
  success: function(cart) {
    // Handle successful response
    console.log('Cart successfully cleared');
  },
  error: function(XMLHttpRequest, textStatus) {
    // Handle error response
    console.error('Error clearing cart: ' + textStatus);
  }
});


In this code snippet, we are sending a POST request to the cart/clear.js endpoint, which will clear the cart. You can include this code in your theme files where you want to implement the functionality to clear the cart using AJAX in Shopify.