Skip to content

Commit

Permalink
TECH-776 Add to cart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ChelseyRiewerTrellis committed Oct 22, 2024
1 parent 9ded90f commit dd914c6
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion assets/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,38 @@ class StickyCompareButton extends HTMLElement {
const compareVariantsAddToCart = document.querySelectorAll('.compare-variant-item__button');
compareVariantsAddToCart.forEach((compareVariantAddToCart) => {
compareVariantAddToCart.addEventListener(('click'), () => {

// Grab qty and id
const qtyInput = compareVariantAddToCart.closest('.compare-variant-item__details').querySelector('.quantity__input');
const qty = parseInt(qtyInput.value);
const variantID = parseInt(qtyInput.getAttribute('data-index'));

const updates = {
'items': [{
'id': variantID,
'quantity': qty
}]
};

// Add item to cart
fetch(window.Shopify.routes.root + 'cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(updates)
})
.then(response => {
compareVariantAddToCart.innerHTML = 'Added!';

setTimeout(() => {
compareVariantAddToCart.innerHTML = 'Add to Cart';
},5000);

return response.json();
})
.catch((error) => {
console.error('Error:', error);
});
});
});
}
Expand Down

0 comments on commit dd914c6

Please sign in to comment.