I figured out how to pass the selected size of a shirt to my cart, but inside the cart the user is unable to adjust their selection:
Here’s my code:
<button id= "addToCartButton" class="snipcart-add-item cart-btn"
data-item-id="Bear Stearns is Fine T-Shirt"
data-item-price="19.99"
data-item-url="/products/bear-stearns-is-fine-shirt"
data-item-description="Soft, tri-blend, unisex fit."
data-item-image="/static/products/shirts/bear stearns is fine shirt closeup.jpg"
data-item-name="Bear Stearns is Fine T-Shirt"
data-item-custom1-name="Size"
data-item-custom1-options="XS|S|M|L|XL|2XL"
>
Add To Cart
</button>
<script>
const addToCart = document.querySelector(".snipcart-add-item");
const quantity = document.querySelector(".quantity");
const increment = document.querySelector(".increment");
const decrement = document.querySelector(".decrement");
const size = document.querySelector(".option-selector");
// Pass size selected to cart. Needs work I think...
size.addEventListener("change", () => {
addToCart.setAttribute("data-item-custom1-options", size.value);
});
// Pass quantity selected to cart
quantity.addEventListener("change", () => {
addToCart.setAttribute("data-item-quantity", quantity.value);
});
// Increment button
increment.addEventListener("click", () => {
if (quantity.value < 9) {
const newQuantity = (parseInt(quantity.value) || 0) + 1;
quantity.value = newQuantity;
addToCart.setAttribute("data-item-quantity", newQuantity);
}
});
// Decrement button
decrement.addEventListener("click", () => {
if (quantity.value > 1) {
const newQuantity = (parseInt(quantity.value) || 0) - 1;
quantity.value = newQuantity;
addToCart.setAttribute("data-item-quantity", newQuantity);
}
});
</script>
Any insight would be much appreciated!