Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CartJS.cart.items delayed #174

Open
dhruveonmars opened this issue Mar 3, 2020 · 12 comments
Open

CartJS.cart.items delayed #174

dhruveonmars opened this issue Mar 3, 2020 · 12 comments

Comments

@dhruveonmars
Copy link

So when I log or run anything against CartJS.cart.items, it always seems to be a step behind CartJS.cart, and I'm not sure how that's possible.

For eg,

After adding an item to the cart, I want to update the cart in the header. To do this, I call a function refreshSidecart(); inside the success function of the CartJS.addItem request.

In refreshSidecart, I am able to log CartJS.cart, which shows the items correctly, however when I log CartJS.cart.items, I get an empty array logged.
If I press the add to cart button again, in the CartJS.cart items, it shows the correct details, with a quantity of 2, however in the CartJS.cart.items log, it only shows 1.

I added the following delay

setTimeout(function() {
    console.log('2', CartJS.cart.items);
  }, 3000);

And this seems to log the correct values.

I'm not sure what to do about this, as I would have thought that CartJS.cart.items would just be accessing the value of items inside CartJS.cart but any help would be greatly appreciated.

@KevinSupertramp
Copy link

Hi there, same problem here. I use the "success" callback to update my cart totals and it is always a step behind. It looks like there is a delay for update the CartJS.cart JSON?

@gavinballard
Copy link
Member

Hi @dhruveonmars and @KevinSupertramp, apologies for the delayed response on this. I have very limited time for Cart.js these days.

If this is still an issue, can you please point me to a publicly-accessible Shopify site that reproduces the issue, ideally as a minimal test case (ie without lots of other third party libraries that may be causing the issue)?

Sounds like there may be some caching issues going on, but I really need a good example case to be able to nut this one out.

@dhruveonmars
Copy link
Author

Hey @gavinballard sure. Will try and get a development site up soon that's publicly accessible and doesn't have the timeouts

@dhruveonmars
Copy link
Author

Hey @gavinballard, Sorry for the delay.

I have a development version of the site I was building setup, and is publicly accessible. https://black-lines-development.myshopify.com/products/test-product.

I need to remove the third-party libraries and the timeouts that were added

@gavinballard
Copy link
Member

Thanks @dhruveonmars! Is the JS code you're using for the sidebar rendering easily accessible?

@dhruveonmars
Copy link
Author

Hi @gavinballard I've added a console.log when you press the 'add to cart' button, so you'll be able to find the code. It looks like Shopify is reformatting the code on the theme publishing, so let me know if it's not quite so readable and easy to follow in the web inspector, and I can upload a copy of the theme.js file which has the 'add to cart' functionality.

I have left the timeouts that were needed on the live store. Please have a look, and let me know if I should remove them

@gavinballard
Copy link
Member

Hey @dhruveonmars, currently looks like some CSS/JS issues on that development site.

@dhruveonmars
Copy link
Author

Hey @gavinballard, sorry about that. Fixed that so you can have a look now

@gavinballard
Copy link
Member

@dhruveonmars can you please try updating to v1.0.2 of Cart.js (just released) to see if the new cache-busting code addresses this problem?

@dhruveonmars
Copy link
Author

hey @gavinballard , I've been testing and it still seems to have a delay. 1.0.2 also seems to require rivets, but I didn't think it was required before

@kevin-ryser
Copy link

Hi everyone, I'm using the latest version (1.1.0) and I can confirm this is still happening.

@kevin-ryser
Copy link

I was able to quickly fix this - not the cleanest - but you'll get the "why" it's happening.

  1. With the update function: I'm using the "success" Ajax callback which is called before the cart update function (= cart.items not updated yet) so I've changed "push" by "unshift" so the cart update is always called first in the Queue.add function:
request.success.push(CartJS.cart.update);

by

request.success.unshift(CartJS.cart.update);
  1. For addItem, addItems functions, I've passed my "success" callback to the "getCart" function instead of the Queue.add:
var cart_options = {};
if (options.success) {
    cart_options.success = options.success;
    options.success = [];
}

CartJS.Queue.add('/cart/add.js', data, options);
return CartJS.Core.getCart(cart_options);

Simply because the loca cart.items is updated with the getCart function.

I hope it makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants