Created
February 8, 2021 10:12
-
-
Save ashukasma/9a4685bf4f18be719a39873fa1099794 to your computer and use it in GitHub Desktop.
Add item to cart in shopify using Fetch API JS
This file contains 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
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