How To Ajaxify Header Cart Items Count In Woocommerce

Share Now

1. Add this code in function.php file:

add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_count');
function wc_refresh_mini_cart_count($fragments){
    ob_start();
    ?>
    <span class="number-of-product-in-store">
        <?php echo WC()->cart->get_cart_contents_count(); ?>
    </span>
    <?php
        $fragments['.number-of-product-in-store'] = ob_get_clean();
    return $fragments;
}

2. Now add these code on the section where you want to show your Cart Items count.

<span class="number-of-product-in-store"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert