Skip to content

Instantly share code, notes, and snippets.

@obrien-k
Created May 13, 2020 16:08
Show Gist options
  • Save obrien-k/ad25f0341112d0b984d7ad011119f9d8 to your computer and use it in GitHub Desktop.
Save obrien-k/ad25f0341112d0b984d7ad011119f9d8 to your computer and use it in GitHub Desktop.
function addMultiToCart(productIds, cartId){
/* Sets the URL to an existing cart id + /items, if not use the cart endpoint to create a new cart */
let url = cartId ?
`/api/storefront/carts/${cartId}/items`:
`/api/storefront/cart`
/* Set a data variable to our lineItems object with the product ids mapped with a quantity of 1 */
let data = {
lineItems: productIds.map(id => ({
quantity: 1,
productId: id
}))
}
console.log(url);
let options = {
method: 'POST',
body: JSON.stringify(data),
credentials: 'include',
headers: {
"Content-Type": "application/json"
}
}
/* Finally we fetch the cart URL with the generated URL and supplied options */
return fetch(url, options)
.then(res => res.json())
.then(json => location.reload()); // Reloads the page
}
const multiButton = document.querySelector('#multiButton');
// Selects the #id that we'll attach the addMultiToCart function to
multiButton.addEventListener('click', event => {
addMultiToCart(arr, cartId) // run the addMultiToCart function with the array we created and a cart ID if ones available in the context.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment