skip to Main Content

Search WordPress Code Snippets – Quick Solutions for Developers


How to Use WooCommerce Refresh Fragment for Better Cart Updates

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;
}


I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

Back To Top