Home › Forums › WoodMart support forum › Mini Cart not updating properly
Mini Cart not updating properly
- This topic has 10 replies, 2 voices, and was last updated 3 years, 2 months ago by
Artem Temos.
-
AuthorPosts
-
February 17, 2022 at 10:23 am #353752
robert_acParticipantHi Team,
I’ve noticed on my site at several key points the mini cart is not updating properly, mainly when a user logs out, or a guest user logs in, their real cart is emptied / merged, but the mini cart doesn’t update until a quantity is changed or item removed, then it updates and or clears itself.
I’ve had a go at fixing it, but it appears the mini cart is loading from sessionStorage, which I’m not very familiar with, is there an ajax function to force that sessionStorage to update when a user performs a specific action?
Thanks for your help,
RobFebruary 17, 2022 at 12:13 pm #353789
Artem TemosKeymasterHello,
Try to add the following PHP code snippet to the child theme functions.php file to fix this
add_filter( 'woodmart_update_fragments_fix', '__return_false' );
Kind Regards
February 17, 2022 at 1:51 pm #353863
robert_acParticipantHi, adding that filter fixes the cart updating when adding and removing items, however it still doesn’t work when a user with a full basket logs out, the cart is empty, but the minicart still shows all the items, is there a function to force the mini cart to update itself?
Or perhaps a way to force sessionStorage to update the current cart info?
February 18, 2022 at 7:11 am #354123
Artem TemosKeymasterHello,
Could you please record a short video so we can better understand the problem?
Kind Regards
February 18, 2022 at 8:46 am #354144
robert_acParticipantHi, I’ve included videos in the extra information for this message.
I know this is somewhat related to something being cached, but there must be a way for me to ajax update the mini cart without writing an entire new function for it? or maybe you know what specifically is being cached so I can exclude it from cache?
Thanks for your help!
February 18, 2022 at 10:05 am #354175
Artem TemosKeymasterHello,
Could you please disable all plugins that are not related to our theme and provide us your admin access so we can check it?
Thank you in advance
February 18, 2022 at 10:48 am #354190
robert_acParticipantHi, Unfortunately I don’t have a staging branch setup at the moment, and I have a lot of sensitive info on our backend on the live site so I can’t give admin access, but I think I’ve located what’s causing the conflict.
In the litespeed cache plugin, the cart can be cached as an ESI block, disabling this fixes the conflict, but does slow the cart loading down quite a lot.
Do you have any idea a workaround I could use for using ESI for the cart data?
Thanks,
RobAttachments:
You must be logged in to view attached files.February 18, 2022 at 11:08 am #354197
robert_acParticipantSo after a little more testing, it’s maybe not related solely to ESI, because if a user logs out, all items remain in the cart even with that setting turned off.
I did notice that something eventually made a call to ?wc-ajax=get_refreshed_fragments which fixes the mini cart, any idea what made that call so I can force it myself at key interactions?
February 18, 2022 at 12:09 pm #354222
Artem TemosKeymasterYes, this request is generated by the WooCommerce plugin to fix your problem. Sorry, but we don’t have a fix to make this call at key interaction only.
Kind Regards
February 18, 2022 at 4:02 pm #354301
robert_acParticipantSo after extensive testing, the problem is related to the sessionStorage not being updated/cleared at certain points logging in and logging out.
I managed to find a workaround for now by manually clearing the sessionStorage on login / logout, this will force woocommerce to call the ajax update fragments and fix them, I have no idea if this is related to woodmart somehow, or just the cache plugin I’m using, but I’ll leave the result in case anyone else can benefit.
var containerNames = ["woocommerce-my-account-wrapper", "wd-dropdown-my-account"]; for (let k = 0; k < containerNames.length; k++) { var logoutContainer = document.getElementsByClassName(containerNames[k]); for (let i = 0; i < logoutContainer.length; i++) { var containerLinks = logoutContainer[i].getElementsByTagName("a"); for (let j = 0; j < containerLinks.length; j++) { if (containerLinks[j].href !== null) { if (containerLinks[j].href.toLowerCase().includes("customer-logout")) { containerLinks[j].onclick = function() { var n = sessionStorage.length; while (n--) { var key = sessionStorage.key(n); if (key.startsWith("wc_fragments_")) { sessionStorage.removeItem(key); } } }; } } } } } var loginLinks = document.getElementsByClassName("woocommerce-form-login__submit"); for (let j = 0; j < loginLinks.length; j++) { loginLinks[j].onclick = function() { var n = sessionStorage.length; while (n--) { var key = sessionStorage.key(n); if (key.startsWith("wc_fragments_")) { sessionStorage.removeItem(key); } } }; }
February 21, 2022 at 7:01 am #354709
Artem TemosKeymasterHello,
We are glad that you sorted it out. Thank you for sharing your solution here on the forum.
Kind Regards
-
AuthorPosts
- You must be logged in to create new topics. Login / Register