• Resolved Tim

    (@thegreentimtam)


    I’m using Woocommerce with WP Rocket.

    My theme has a Cart at the top of the page, and I’ve added some code to update the cached cart number with the new number of items using AJAX.

    add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );
    
    function woocommerce_header_add_to_cart_fragment( $fragments ) {
    	global $woocommerce;
    
    	ob_start();
    
    	?>
    	<a href="<?php echo esc_url(wc_get_cart_url()); ?>" class="et-cart-info"><span><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?></span></a>
    	<?php
    	$fragments['div#et-secondary-menu a.et-cart-info'] = ob_get_clean();
    	return $fragments;
    }

    It seems like this only updates when there are items in the cart. Sometimes the page is cached when there are items in the cart. So, if a user visits a page cached with “3 items”, they are shown “3 items” when it should say “0 items”.

    Is it possible to update the text to say “0 items” when nothing is in the cart with AJAX?

Viewing 1 replies (of 1 total)
  • Thread Starter Tim

    (@thegreentimtam)

    RESOLVED:

    The theme has a function with the following line:

    $items_number = WC()->cart->get_cart_contents_count();

    I simply copied that function into my child theme with the line replaced with:

    $items_number = 0;

    Now the page will always be cached with “0 items”, and is replaced with AJAX when items are in the cart.

Viewing 1 replies (of 1 total)
  • The topic ‘Question about AJAX and Caching’ is closed to new replies.