Last active
August 10, 2022 06:57
-
-
Save m-muhsin/e6b8bfcbb27aa793d4bf41751d942456 to your computer and use it in GitHub Desktop.
Add Multiple Products to Cart using Promises.
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
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