Buy All 47 Premium Themes For the Price Of $199.99

How to Add a Custom “Live Cart Item Counter” in WooCommerce Using Hooks

A live cart item counter is a simple but highly effective UX improvement.
It shows customers how many items are currently in their cart in real time, increasing awareness and encouraging checkout.

In this tutorial, you will learn how to display and update a live cart counter in WooCommerce using hooks.

Why a Cart Counter Improves Conversions

When users see their cart updating dynamically, it creates a sense of progress and engagement.

  • Encourages checkout completion
  • Improves navigation awareness
  • Increases engagement
  • Works especially well on mobile

The WooCommerce Hook We Will Use

We will use:

woocommerce_add_to_cart_fragments

This hook allows WooCommerce to update parts of the page via AJAX without reloading.

Step 1: Add Cart Counter Markup

Add this to your theme’s functions.php file or custom plugin:


add_action('wp_footer', 'add_cart_counter_html');

function add_cart_counter_html() {

    echo '
'; echo 'Cart: ' . WC()->cart->get_cart_contents_count() . ''; echo '
'; }

Step 2: Enable Live Updates

Now we make the counter update dynamically:


add_filter('woocommerce_add_to_cart_fragments', 'update_cart_counter_fragment');

function update_cart_counter_fragment($fragments) {

    $fragments['span.count'] = '' . WC()->cart->get_cart_contents_count() . '';

    return $fragments;
}

How It Works

  • Displays cart item count on all pages
  • Updates automatically after adding products
  • Uses WooCommerce AJAX fragments system

Customization Ideas

  • Add cart total price next to item count
  • Animate counter on update
  • Place counter in header menu
  • Change color based on cart size