Ensure your cart updates dynamically with AJAX. This function handles WooCommerce refresh fragment for a smooth user experience.
add_filter('woocommerce_add_to_cart_fragments', 'custom_cart_fragment');
function custom_cart_fragment($fragments) {
ob_start();
?>
<span class="custom-cart-item-count"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
<?php
$fragments['span.custom-cart-item-count'] = ob_get_clean();
return $fragments;
}