1

NB: Make sure you do a back up of your theme, files and database before attempting the tutorials

WooCommerce : statistiques des paniers actifs
Intermediate
Web Hosting Canada

Last modified : Aug 05 2026

Estimated reading time : 3 minutes, 8 seconds - 181 words

In this tutorial, we’ll learn how to calculate the total number of products currently stored in active WooCommerce shopping carts, as well as the total value of all active carts.

WooCommerce’s built-in analytics allow you to track orders, sales, and revenue, but they don’t always provide a real-time view of customer activity. However, the products currently added to visitors’ shopping carts are valuable data that can help you analyze customer interest and estimate your potential sales before purchases are completed.

By using WooCommerce active cart statistics, you can determine the total number of items currently selected by customers and the combined monetary value of all active shopping carts before they are converted into orders.

In this tutorial, we’ll create two custom functions to:

  • Calculate the total quantity of products across all active WooCommerce shopping carts.
  • Calculate the total value of all active WooCommerce shopping carts.

Calculate the Total Quantity of Products in All Active WooCommerce Shopping Carts.

/////////////////////////// Show count product in all carts  /////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

function woocl_get_total_products_in_active_carts() {
    global $wpdb;

    // 1. On récupère dynamiquement le bon ID du site (généralement 1)
    $blog_id = get_current_blog_id();
    $meta_key = '_woocommerce_persistent_cart_' . $blog_id;

    // 2. On exécute la requête avec la bonne clé
    $results = $wpdb->get_results($wpdb->prepare("
        SELECT meta_value 
        FROM {$wpdb->usermeta} 
        WHERE meta_key = %s AND meta_value != ''
    ", $meta_key));

    $total = 0;

    if (!empty($results)) {
        foreach ($results as $row) {
            $cart_data = maybe_unserialize($row->meta_value);

            // 3. On vérifie s'il y a des produits et on additionne les quantités
            if (!empty($cart_data['cart']) && is_array($cart_data['cart'])) {
                foreach ($cart_data['cart'] as $item) {
                    if (isset($item['quantity']) && is_numeric($item['quantity'])) {
                        $total += (int) $item['quantity'];
                    }
                }
            }
        }
    }

    return $total;
}

Calculate the Total Value of All Active WooCommerce Shopping Carts.

/////////////////////////// Show price amount in all carts  //////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////

function woocl_get_total_cart_amount_all_users() {
    global $wpdb;

    $blog_id = get_current_blog_id();
    $meta_key = '_woocommerce_persistent_cart_' . $blog_id;

    // Récupération de toutes les lignes non vides
    $results = $wpdb->get_results($wpdb->prepare("
        SELECT meta_value 
        FROM {$wpdb->usermeta} 
        WHERE meta_key = %s AND meta_value != ''
    ", $meta_key));

    $total_amount = 0;

    if (!empty($results)) {
        foreach ($results as $row) {
            $cart_data = maybe_unserialize($row->meta_value);

            // On vérifie s'il y a bien des éléments dans le panier (ex: a:2:{...} et non a:0:{})
            if (!empty($cart_data['cart']) && is_array($cart_data['cart'])) {
                
                foreach ($cart_data['cart'] as $cart_item_key => $item) {
                    
                    // WooCommerce stocke TOUJOURS l'ID dans 'product_id' au sein de l'élément
                    $product_id = isset($item['product_id']) ? (int) $item['product_id'] : 0;
                    $quantity = isset($item['quantity']) ? (int) $item['quantity'] : 0;

                    if ($product_id > 0 && $quantity > 0) {
                        $product = wc_get_product($product_id);
                        
                        if ($product) {
                            // Prise en compte du prix du produit (ou de la variation)
                            $price = (float) $product->get_price();
                            $total_amount += $price * $quantity;
                        }
                    }
                }
            }
        }
    }

    return wc_price($total_amount);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>