Skip to content

Instantly share code, notes, and snippets.

@m-muhsin
Last active August 10, 2022 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-muhsin/e6b8bfcbb27aa793d4bf41751d942456 to your computer and use it in GitHub Desktop.
Save m-muhsin/e6b8bfcbb27aa793d4bf41751d942456 to your computer and use it in GitHub Desktop.
Add Multiple Products to Cart using Promises.
const productsToAdd = [
{ 'add-to-cart': 68, 'quantity': 2 },
{ 'add-to-cart': 69, 'quantity': 3 },
{ 'add-to-cart': 70, 'quantity': 4 }
];
for (const product of productsToAdd) {
try {
const response = await fetch(window.location.pathname, {
method: 'POST',
body: new URLSearchParams(product),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
if (response.ok) {
console.log('Product added to cart:', product['add-to-cart']);
}
} catch (e) {
console.error(e);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment