Get the number of items in WooCommerce cart

Get the number of items in WooCommerce cart

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Getting Cart Item Counts Without Totals in WooCommerce

Displaying a cart counter on your site is a standard UX pattern, but achieving it without leaking pricing data requires specific filtering. In WooCommerce, the default cart widget or menu hook often renders the total amount alongside the item count. If you are building a custom navigation menu—perhaps using Avada for theme features but maintaining your own markup—you likely want to isolate the quantity logic from the financial calculations.

To get strictly the number of items in the cart, you need to tap into WooCommerce's action hooks and filter the output before it reaches the browser. The core mechanism involves hooking into woocommerce_cart_contents_count or creating a custom function that calculates the count directly from the cart session.

The most robust approach for a developer is to calculate the value programmatically rather than relying on a pre-built widget that includes HTML tags you might not want. You can achieve this by adding a snippet to your theme's functions.php file or a custom plugin. The logic relies on accessing the global WC_Cart object, which persists throughout the session.

Here is a pattern for calculating the raw count:

function my_custom_cart_item_count() {
    if ( class_exists( 'WC_Cart' ) && WC()->cart->is_empty() === false ) {
        $count = 0;
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $count += $cart_item['quantity'];
        }
        return $count;
    }

    return 0;
}

This function iterates through the get_cart() array, which contains keys for each product instance. By summing the quantity key for every item, you derive the total number of physical units in the cart. This method ensures you are not dependent on specific display filters and gives you a clean integer to manipulate.

Once you have this count, you can output it inside your custom menu. If you are using an image link as your "Go to Cart" button, you might want to wrap the image with a badge or overlay text. You could use PHP logic like this:

$count = my_custom_cart_item_count();
if ( $count > 0 ) {
    // Echo the image link here
    echo '<a href="' . wc_get_cart_url() . '" class="cart-link">';
        // Your image HTML goes here
        echo '<img src="/path/to/cart-icon.svg" alt="Cart">';
        echo '</a>';
    
    // Display the number, perhaps with a badge style
    echo '<span class="cart-count">' . esc_html( $count ) . '</span>';
}

A critical consideration when implementing this is escaping output. Always use esc_html() or esc_attr() on variables derived from user sessions or calculated dynamically to prevent XSS vulnerabilities. Since the count comes from the cart session, it is inherently safe in terms of data source, but rendering it in HTML requires sanitization.

Another potential pitfall involves caching. If you are using object caching (Redis or Memcached) with WooCommerce, ensure that the cart state isn't being cached globally. Cart contents are user-specific and change frequently; caching a WC_Cart instance can lead to stale counts where visitors see an old item total from another user's session. Stick to server-side rendering for cart logic unless you have a very specific performance requirement and understand the cache invalidation rules.

Finally, if you are integrating with external tools or need to share this count via AJAX, consider using the wc_add_to_cart or remove_from_cart hooks to update your counter dynamically without reloading the page. However, for static menu items like a "Go to Cart" link, calculating the count on the server side per request is often more reliable and simpler to maintain than managing complex JavaScript state for cart updates.

Our network