Search code examples
phpsqlwordpresswoocommerceorders

Get processing status orders count in WooCommerce?


I want to get the processing order count in WooCommerce. I'm using the following code in the Code Snippet plugin but that is working.

if( !function_exists( 'wc_processing_order_count' ) ) { 
    require_once '../plugins/woocommerce/includes/wc-order-functions.php'; 
}


// NOTICE! Understand what this does before running. 
$result = wc_processing_order_count(); 

It's returning nothing.


Solution

  • This custom function use a very light SQL query to get the orders count from a specific status:

    function get_orders_count_from_status( $status ){
        global $wpdb;
    
        // We add 'wc-' prefix when is missing from order staus
        $status = 'wc-' . str_replace('wc-', '', $status);
    
        return $wpdb->get_var("
            SELECT count(ID)  FROM {$wpdb->prefix}posts WHERE post_status LIKE '$status' AND `post_type` LIKE 'shop_order'
        ");
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    Usage example for "processing" orders count:

    // Display "processing" orders count
    echo get_orders_count_from_status( "processing" );