Ajax based header cart items count in Woocommerce

If you are struggling to display cart count in the small header widget, then here is the classic way of doing it. Woocommerce has inbuilt feature to refresh the fragments.

1) The HTML to be refreshed: So first in your theme’s header.php file you should need to embed the cart count in a specific html tag with a defined unique ID (or a class), for example something like:

<div id="mini-cart-count"></div>

2) The code: Put in functions.php

add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_count');
function wc_refresh_mini_cart_count($fragments){
    <div id="mini-cart-count">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
        $fragments['#mini-cart-count'] = ob_get_clean();
    return $fragments;

This will automatically refresh the count as soon as item is added to cart. If yo want to forcefully refresh the count then :


Leave a Reply

Your email address will not be published. Required fields are marked *