Re: Fetch getting previous data

Solved

Fetch getting previous data

CozyMama
Tourist
6 0 10

I am trying to fetch the current contents of my cart after a change in quantity is made. Let's say I have two products in my cart. Product A has QTY 3 and Product B has Quantity 5. 

In my code, I am logging to the console A.QTY + B.QTY. In this scenario, it should equal 8. 

If I come in and increase Product A to QTY 4. The console still outputs 8 (when it should be 4 + 5 = 9). 

 

If I go increase Product A to QTY 5, the console outputs 9 (the answer I was looking for last time - but this time, I'd want 5 + 5 = 10). Anyone help me out here?

Here's my current code (in cart.js on the Dawn theme):

 

onchange(event) {

var Num = 0; //used to count sum of product

this.updateQuantity(event.target.dataset.index, event.target.value, document.activeElement.getAttribute('name')); console.log (event.target.value); // Verify that event.target.value is the new QTY in the cart after the change, not the previous


fetch('/cart.js') // fetches data (should be the current data, not the data before the event change is my understanding
.then(resp => resp.json()) // Transform the data into json
.then(function (data) {
let cart = data;

for (let index = 0; index < cart.items.length; index++) {

Num = Num + cart.items[index].quantity; // add up the quantities of each product listed in cart
console.log(Num); // log result for every loop interation


     } // end index for

  }); // end fetch then

} // end OnChange Event

 

My end goal is to receive the correct current quantities of the products in my cart right after the event change as it seems with fetch, I'm only getting the data before the change was made. 


I'm open to any help possible. I've tried clearing cache, but maybe I'm doing that wrong so I've removed it from my code. Please help me out here!

Accepted Solution (1)

PaulNewton
Shopify Partner
6405 584 1358

This is an accepted solution.

this.updateQuantity may still be resolving, when your attempt to fetch goes out so you are getting back the current data, just not the updated data.

 

Instead of onchange try using the PUB/SUB system in dawn subscribing the code to PUB_SUB_EVENTS.cartUpdate that happens after the updateQuantity function get  it's response

https://github.com/Shopify/dawn/blob/main/assets/cart.js#L170 

for the subscribing pattern  see https://github.com/Shopify/dawn/blob/main/assets/cart.js#L31C1-L36C8 

 

Though to be sure you'd want to step through it the browsers devtools debugger , and observe the network calls.

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


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

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


View solution in original post

Replies 2 (2)

PaulNewton
Shopify Partner
6405 584 1358

This is an accepted solution.

this.updateQuantity may still be resolving, when your attempt to fetch goes out so you are getting back the current data, just not the updated data.

 

Instead of onchange try using the PUB/SUB system in dawn subscribing the code to PUB_SUB_EVENTS.cartUpdate that happens after the updateQuantity function get  it's response

https://github.com/Shopify/dawn/blob/main/assets/cart.js#L170 

for the subscribing pattern  see https://github.com/Shopify/dawn/blob/main/assets/cart.js#L31C1-L36C8 

 

Though to be sure you'd want to step through it the browsers devtools debugger , and observe the network calls.

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


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

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


CozyMama
Tourist
6 0 10

Thanks @PaulNewton. I took what you said and altered it a little bit. 

 

Instead of subscribing my function, I just called my function after the 
publish(PUB_SUB_EVENTS.cartUpdate, { source: 'cart-items' });
line executes in the UpdateQuantity function in my cart.js file. In calling my entire function right after this line executed, it ensured that the cart was updated and then pulled the information appropriately as I wanted. 

Thanks for given me the direction that the UpdateQuantity may still be resolving. I didn't think about that. Really helpful!