Query WooCommerce orders count and total amount for specific status via SQL (HPOS compatible)

I’m developing a custom WordPress plugin to display WooCommerce order statistics in the admin panel using SQL queries. However, the plugin does not show the correct data.

Issue:

The plugin is supposed to display the total number of completed and processing orders, along with their total amount and total number of canceled orders and their amount. However, the displayed statistics are incorrect or not showing up at all.

Expected Output:

  • Total Number of Orders: [Expected number]

  • Total Amount for Total Orders: [Expected amount]

  • Total Cancelled Orders: [Expected number]

  • Total Amount for Cancelled Orders: [Expected amount]

<?php
/*
Plugin Name: WooCommerce Order Stats
Description: Displays order statistics for WooCommerce in the WordPress admin panel.
Version: 1.0
Author: Your Name
*/

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Function to display WooCommerce order statistics
function display_woocommerce_order_stats_admin_notice() {
    global $wpdb;
    $table_prefix = $wpdb->prefix;
    $orders_table = "{$table_prefix}wc_orders"; // WooCommerce orders table name

    // Query total number of completed orders
    $total_completed_orders = $wpdb->get_var("
        SELECT COUNT(ID)
        FROM {$orders_table}
        WHERE post_status = 'wc-completed'
    ");

    // Query total amount for completed orders
    $total_completed_amount = $wpdb->get_var("
        SELECT SUM(meta.meta_value)
        FROM {$orders_table} AS orders
        INNER JOIN {$table_prefix}postmeta AS meta ON orders.ID = meta.post_id
        WHERE orders.post_status = 'wc-completed'
        AND meta.meta_key = '_order_total'
    ");

    // Query total number of processing orders
    $total_processing_orders = $wpdb->get_var("
        SELECT COUNT(ID)
        FROM {$orders_table}
        WHERE post_status = 'wc-processing'
    ");

    // Query total amount for processing orders
    $total_processing_amount = $wpdb->get_var("
        SELECT SUM(meta.meta_value)
        FROM {$orders_table} AS orders
        INNER JOIN {$table_prefix}postmeta AS meta ON orders.ID = meta.post_id
        WHERE orders.post_status = 'wc-processing'
        AND meta.meta_key = '_order_total'
    ");

    // Query total number of cancelled orders
    $total_cancelled_orders = $wpdb->get_var("
        SELECT COUNT(ID)
        FROM {$orders_table}
        WHERE post_status = 'wc-cancelled'
    ");

    // Query total amount for cancelled orders
    $total_cancelled_amount = $wpdb->get_var("
        SELECT SUM(meta.meta_value)
        FROM {$orders_table} AS orders
        INNER JOIN {$table_prefix}postmeta AS meta ON orders.ID = meta.post_id
        WHERE orders.post_status = 'wc-cancelled'
        AND meta.meta_key = '_order_total'
    ");

    // Calculate total orders and total amount
    $total_orders = $total_completed_orders + $total_processing_orders;
    $total_order_amount = $total_completed_amount + $total_processing_amount;

    // Output the statistics and table name as an admin notice
    echo '<div class="notice notice-info">';
    echo '<p><strong>WooCommerce Order Statistics (Table: ' . $orders_table . '):</strong></p>';
    echo '<ul>';
    echo '<li>Total Number of Orders: ' . $total_orders . '</li>';
    echo '<li>Total Amount for Total Orders: ' . wc_price($total_order_amount) . '</li>';
    echo '<li>Total Cancelled Orders: ' . $total_cancelled_orders . '</li>';
    echo '<li>Total Amount for Cancelled Orders: ' . wc_price($total_cancelled_amount) . '</li>';
    echo '</ul>';
    echo '</div>';
}

// Hook into the admin menu
add_action('admin_notices', 'display_woocommerce_order_stats_admin_notice');

0

There are some mistakes in your SQL queries:

  • The index column in wc_orders table is id but not ID,
  • You don’t need to JOIN another table to get order total sum, as you can use wc_orders table total_amount column,
  • You can group orders for multiple statuses in one query.

So replace your function with:

function display_woocommerce_order_stats_admin_notice() {
    global $wpdb;
    
    // Query total number of completed orders
    $total_paid_orders = $wpdb->get_var("
        SELECT COUNT(id)
        FROM {$wpdb->prefix}wc_orders
        WHERE status IN ('wc-completed', 'wc-processing')
    ");
    
    // Query total amount for completed orders
    $total_paid_orders_amount = $wpdb->get_var("
        SELECT SUM(total_amount)
        FROM {$wpdb->prefix}wc_orders
        WHERE status IN ('wc-completed', 'wc-processing')
    ");
    
    // Query total number of cancelled orders
    $total_cancelled_orders = $wpdb->get_var("
        SELECT COUNT(id)
        FROM {$wpdb->prefix}wc_orders
        WHERE status = 'wc-cancelled'
    ");
    
    // Query total amount for cancelled orders
    $total_cancelled_orders_amount = $wpdb->get_var("
        SELECT SUM(total_amount)
        FROM {$wpdb->prefix}wc_orders
        WHERE status = 'wc-cancelled'
    ");

    // Output the statistics and table name as an admin notice
    echo '<div class="notice notice-info">
    <p><strong>' . __('WooCommerce Order Statistics (Table: wc_order)') . ':</strong></p>
    <ul>
        <li>' . __('Total Number of Orders') . ': ' . $total_paid_orders . '</li>
        <li>' . __('Total Amount for Total Orders') . ': ' . wc_price($total_paid_orders_amount) . '</li>
        <li>' . __('Total Cancelled Orders') . ': ' . $total_cancelled_orders . '</li>
        <li>' . __('Total Amount for Cancelled Orders') . ': ' . wc_price($total_cancelled_orders_amount) . '</li>
    </ul></div>';
}
add_action('admin_notices', 'display_woocommerce_order_stats_admin_notice');

It should better work

You should target a specific page in your code, to avoid those queries to be executed each time you browse a page in the admin.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật