• mygain

    (@mygain)


    Hi!
    The wishlist counter in the header gets cached by WP Rocket.

    When I add a product to the wishlist the counter updates. But when navigating to another page the count goes back again. In general settings Ajax is turned on.

    When I clear WP Rocket’s cache the count updates and show correct number. But navigating to any other page show’s the incorrect count (typical cache issue).

    The Cart icon does not get cached and works correctly.

    I have tried with User Cache on and off. Same results.

    I rather don’t exclude the YITH wishlist cookie because it slows the page a lot.
    I thought the ajax option was precisely for this? The cart count works just fine, the wishlist doesn’t.

    YITH WooCommerce Wishlist V3.0.18

    Kind regards!

    • This topic was modified 4 years ago by mygain.
    • This topic was modified 4 years ago by mygain.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author YITHEMES

    (@yithemes)

    Hi there

    This looks like something added by your theme; if you want your counter to be update, even when page is served via cache, you’ll need to refresh it via ajax

    I suggest you to add this snippet of code at the end of functions.php file of your theme or child

    if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_ajax_update_count' ) ) {
    	function yith_wcwl_ajax_update_count() {
    		wp_send_json(
    			array(
    				'count' => yith_wcwl_count_all_products(),
    			)
    		);
    	}
    	add_action( 'wp_ajax_yith_wcwl_update_wishlist_count', 'yith_wcwl_ajax_update_count' );
    	add_action( 'wp_ajax_nopriv_yith_wcwl_update_wishlist_count', 'yith_wcwl_ajax_update_count' );
    }
    
    if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_enqueue_custom_script' ) ) {
    	function yith_wcwl_enqueue_custom_script() {
    		wp_add_inline_script(
    			'jquery-yith-wcwl',
    			"
            jQuery( function( $ ) {
              let refreshCounter =  () => {
                $.get( yith_wcwl_l10n.ajax_url, {
                  action: 'yith_wcwl_update_wishlist_count'
                }, function( data ) {
                  $('.menu-item-wishlist').find('.mini-item-counter').html( data.count );
                } );
              };
              
              $( document ).on( 'added_to_wishlist removed_from_wishlist', refreshCounter );
              
              refreshCounter();
            } );
          "
    		);
    	}
    	add_action( 'wp_enqueue_scripts', 'yith_wcwl_enqueue_custom_script', 20 );
    }
    

    This will force counter to be updated every time something is added/removed from wishlist, and even when page gets loaded

Viewing 1 replies (of 1 total)
  • The topic ‘Wishlist in header gets cached (ajax on)’ is closed to new replies.