Force cart object to update

Solved

Force cart object to update

Tjark
Shopify Partner
5 0 2

My Shopify App adds a freshly created variant to the cart: 

 

let formData = {
  'items': [{
    'id': number,
    'quantity': quantity,
     'properties': properties
  }]
};

console.log("formData:", formData);

fetch('/cart/add.js', {
  method: 'POST',
  headers: {
     'Content-Type': 'application/json'
  },
body: JSON.stringify(formData)
})
.then(async response => {
  const fulfilledResponse = await response.json();
  console.log("Cart response4: ", fulfilledResponse);
 
  //Wait for the cart to be updated
  await waitForCardToBeUpdated(number);
  document.getElementById('vz_atc_btn').classList.remove('action_button__loading');
  window.location.href = '/cart';
})
.catch((error) => {
  console.error('Error:', error);
});
 
In the waitForCardToBeUpdated function, I check, if the price of the variant is correct in the cart. This is not the case for the first 7 to 10 seconds for the fresh variant.
 
async function waitForCardToBeUpdated(variantId, maxRetries = 10, delay = 5000) {
  for (let i = 0; i < maxRetries; i++) {
    const response = await fetch(`/cart.js`);

    const cart = await response.json();

    //Check if the cart has been updated (Check if the price of the added item is the calculated price)
    let isCartUpdated = false;
    cart.items.forEach(item => {
      if (Number(item.variant_id) === Number(variantId)) {
        if (Number(item.price) === Number((calcPrice() * 100))) {
          console.log("Cart updated");
          isCartUpdated = true;
        }
      }
    });
    if (isCartUpdated) {
      return;
    }
  await new Promise(resolve => setTimeout(resolve, delay));
  }
throw new Error('Cart not updated in time');
}
 
Is there a way to force an update on the cart object before I redirect to it?

Accepted Solution (1)

PaulNewton
Shopify Partner
7721 678 1619

This is an accepted solution.

Your asking for time travel, the cart is updating there is nothing to force, you just have to wait.

If there is a wait due to variants being created that is an app process issue not an ajax api issue.

 

Try premaking placeholder variants, like stubs for editing, so variants don't have to start a creation process you just update the variant with relevant values.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


View solution in original post

Replies 2 (2)

PaulNewton
Shopify Partner
7721 678 1619

This is an accepted solution.

Your asking for time travel, the cart is updating there is nothing to force, you just have to wait.

If there is a wait due to variants being created that is an app process issue not an ajax api issue.

 

Try premaking placeholder variants, like stubs for editing, so variants don't have to start a creation process you just update the variant with relevant values.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


Tjark
Shopify Partner
5 0 2

Thanks for your reply. Placeholder variants and then changing the price was excatly the solution.