Hi everyone!
I want to offer an upsell on the PDP, with another button. This button will add a different item to the cart, and the standard add to cart notification with the product added should show up.
So for example, I might be on the pdp for Product A, but when pressing the upsell, it adds Product B, with the add to cart notification showing Product B has been added.
I’m using Dawn theme. I tried to use the cart-notification ID, but I don’t think I’m using it properly.
This is the function I’m using to handle the button click:
function handleAddToCartClick(event) {
event.preventDefault();
fetch('/cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: membershipVariantId,
quantity: 1,
selling_plan: sellingPlanId
})
})
.then(response => response.json())
.then(data => {
if (data.status === 422) {
alert('Could not add product to cart.');
} else {
// Fetch the updated cart data (optional)
fetch('/cart.js')
.then(response => response.json())
.then(cartData => {
// Show the cart notification popup
const cartPopup = document.getElementById('cart-notification');
if (cartPopup) {
cartPopup.style.display = 'block';
}
});
}
})
.catch(error => {
console.error('Error adding product to cart:', error);
});
}
Thanks for your help in advance!