Home Forums WoodMart support forum Mini Cart not updating properly

Mini Cart not updating properly

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #353752

    robert_ac
    Participant

    Hi 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,
    Rob

    #353789

    Artem Temos
    Keymaster

    Hello,

    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

    #353863

    robert_ac
    Participant

    Hi, 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?

    #354123

    Artem Temos
    Keymaster

    Hello,

    Could you please record a short video so we can better understand the problem?

    Kind Regards

    #354144

    robert_ac
    Participant

    Hi, 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!

    #354175

    Artem Temos
    Keymaster

    Hello,

    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

    #354190

    robert_ac
    Participant

    Hi, 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,
    Rob

    Attachments:
    You must be logged in to view attached files.
    #354197

    robert_ac
    Participant

    So 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?

    #354222

    Artem Temos
    Keymaster

    Yes, 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

    #354301

    robert_ac
    Participant

    So 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);
    			}
    		}
    	};
    }
    #354709

    Artem Temos
    Keymaster

    Hello,

    We are glad that you sorted it out. Thank you for sharing your solution here on the forum.

    Kind Regards

Viewing 11 posts - 1 through 11 (of 11 total)