Created
July 7, 2022 03:07
-
-
Save hezninja/962974cb8dc87e5cf4cdf9dd434afa62 to your computer and use it in GitHub Desktop.
Shopify Ajax API - Add Item to cart
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
<script> | |
var items = ['id': VARIANT_ID,'quantity': 1]; | |
function addToCart(items){ | |
let formData = { | |
'items': items | |
}; | |
fetch(window.Shopify.routes.root + 'cart/add.js', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(formData) | |
}) | |
.then(response => { | |
alert('Added Product To Cart'); | |
return response.json(); | |
}) | |
.catch((error) => { | |
console.error('Error:', error); | |
}); | |
} | |
</script> | |
<button onclick="addToCart(items)">Add to Cart</button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment