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!