Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created November 10, 2022 15:10
Show Gist options
  • Save ihorduchenko/cbcea8ed1e07aaa35440736022a6c504 to your computer and use it in GitHub Desktop.
Save ihorduchenko/cbcea8ed1e07aaa35440736022a6c504 to your computer and use it in GitHub Desktop.
Ajax methods to manage cart items in Shopify
// Add item to the cart
function cartItemAdd(id, qty) {
fetch('/cart/add.js', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify({'items': [{'id':id,'quantity':qty}]})
})
.then(response => {
return response.json();
})
.catch((error) => {
console.error('Error:', error);
alert('cartAdd');
alert(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment