Created
November 10, 2022 15:10
-
-
Save ihorduchenko/cbcea8ed1e07aaa35440736022a6c504 to your computer and use it in GitHub Desktop.
Ajax methods to manage cart items in Shopify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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