Hello, I am trying to retrieve the quantity data each time someone updates the cart so I can pass the quantity amount to an external checkout URL (ClickBank). I know every time someone updates the quantity in the cart, it is posted to /cart/change.js. The issue is I don’t understand how to get that data after someone updates the cart. I am able to get the proper data from cart.json using fetch, but I can’t fetch /cart/change.js after the cart is updated.
Any help is MUCH appreciated. I’ve been stuck on this problem for so long. Literally all I have to do is have the clickbank URL update with the quantity that is in change.js.
The ajax change endpoint is for changing the cart data so wouldn’t be one you’d query directly for getting cart contents. That would still be the cart.js endpoint.
Can you help explain to me how I would implement that in the code I already have? Basically, I have a code that adds parameters to the checkout button based on the product and quantity. What I need to do is update the quantity every time someone updates the quantity in the cart. I am new to JS and Shopify so help is appreciated See my code below:
function updateCartButton() {
// Fetch Cart JSON from Shopify
const cartJson = '/cart.json';
async function getCartJson() {
fetch(cartJson)
.then((res) => {
return res.json();
})
.then((data) => {
// Get Product Info from Shopify
let productInfo = data.items;
console.log(productInfo);
// Create URL Params Array
let urlParamsArray = [];
const createURL = () => {
productInfo.forEach(object => {
const urlParams = `${object.sku}.${object.quantity}`;
urlParamsArray.push(urlParams);
})
}
createURL()
console.log(urlParamsArray);
// Join URL Params into One String
let urlParamsJoin = urlParamsArray.join('_');
console.log(urlParamsJoin);
// Get Checkout Button from Cart
const checkoutBtn = document.querySelector('.cart__submit');
console.log(checkoutBtn);
// Add URL Params to Checkout Button
checkoutBtn.onclick = () => {
location.href = `https://cleannutra.pay.clickbank.net/?cbitems=` + `${urlParamsJoin}`;
}
});
}
getCartJson();
}
updateCartButton();
Hi LitExtension that is not what I’m asking. I’m asking to fetch the updated quantity of each product after the quantity is updated. cart-item_count does not update in cart.json. Please see a screen recording in the link below. How do I fetch the updated quantities from /change.js?