Skip to content

Instantly share code, notes, and snippets.

@ashukasma
Created February 8, 2021 10:12
Show Gist options
  • Save ashukasma/9a4685bf4f18be719a39873fa1099794 to your computer and use it in GitHub Desktop.
Save ashukasma/9a4685bf4f18be719a39873fa1099794 to your computer and use it in GitHub Desktop.
Add item to cart in shopify using Fetch API JS
var VID = 0;
var addData = {
'id':VID,
'quantity':1
};
fetch('/cart/add.js', {
body: JSON.stringify(addData),
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
'X-Requested-With':'xmlhttprequest' /* XMLHttpRequest is ok too, it's case insensitive */
},
method: 'POST'
}).then(function(response) {
return response.json();
}).then(function(json) {
/* we have JSON */
console.log(json)
}).catch(function(err) {
/* uh oh, we have error. */
console.error(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment