Get quantity data from /cart/change.js after quantity is updated

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.

Hi @mkaskie ,

You can refer: https://github.com/Shopify/dawn/blob/main/assets/cart.js

When you do change.js, it will return state value and you can use it.

Hope it helps!

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 :slightly_smiling_face: 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 @mkaskie ,

You can refer

you can use data.item_count , it will be the number of items in the cart.

Refer https://shopify.dev/api/liquid/objects#cart-item_count

Hope it helps!

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?

https://www.awesomescreenshot.com/video/10386564?key=7cdb64aa69b3cb8ed97d734b5e3b628f

Hi @mkaskie ,

Please send your site and if your site is password protected, please send me the password. I will check it.

Hi @LitExtension thanks! Here it is:

https://cleannutra.com/pages/ads-demo

https://cleannutra.com/cart

You’ll see an add to cart button above the products on the demo page. Please use that button to test. Thanks again!!

Hi @LitExtension can you still help me out with this?