Search code examples
phpwordpresswoocommercedashboardorders

Adding custom order statuses in Admin Dashboard Stats Widget


I would like to include details from custom order status in WooCommerce Admin Dashboard Stats widget. I have set 2 custom order status which comes after wc-processing.

Order Flow after successful payment is:
wc-processing => wc-awaiting-shipment => wc-dispatched => wc-completed.

As awaiting shipment and dispatched are custom order statuses, WooCommerce stats widget is not reflecting those orders in total sales amount. The problem is that I have many orders with wc-dispatched and wc-awaiting-shipment statuses.

This is code that I have used to register this custom order statuses:

/**
 * Register new status
 * Tutorial: http://www.sellwithwp.com/woocommerce-custom-order-status-2/
 * */
function register_awaiting_shipment_order_status() {
    register_post_status('wc-awaiting-shipment', array(
        'label' => 'Awaiting Shipment',
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>')
    ));
}

add_action('init', 'register_awaiting_shipment_order_status');

// Add to list of WC Order statuses
function add_awaiting_shipment_to_order_statuses($order_statuses) {

    $new_order_statuses = array();

    // add new order status after processing
    foreach ($order_statuses as $key => $status) {
        $new_order_statuses[$key] = $status;
        if ('wc-processing' === $key) {
            $new_order_statuses['wc-awaiting-shipment'] = 'Awaiting shipment';
        }
    }
    return $new_order_statuses;
}

add_filter('wc_order_statuses', 'add_awaiting_shipment_to_order_statuses');

/**
 * Register new status
 * Tutorial: http://www.sellwithwp.com/woocommerce-custom-order-status-2/
 * */
function register_dispatched_order_status() {
    register_post_status('wc-dispatched', array(
        'label' => 'Dispatched',
        'public' => true,
        'exclude_from_search' => false,
        'show_in_admin_all_list' => true,
        'show_in_admin_status_list' => true,
        'label_count' => _n_noop('Dispatched <span class="count">(%s)</span>', 'Dispatched <span class="count">(%s)</span>')
    ));
}

add_action('init', 'register_dispatched_order_status');

// Add to list of WC Order statuses
function add_dispatched_to_order_status($order_status) {

    $new_order_statuses = array();

    // add new order status after processing
    foreach ($order_status as $key => $status) {

        $new_order_statuses[$key] = $status;

        if ('wc-awaiting-shipment' === $key) {
            $new_order_statuses['wc-dispatched'] = 'Dispatched';
        }
    }

    return $new_order_statuses;
}

add_filter('wc_order_statuses', 'add_dispatched_to_order_status');

Woocommerce Status Dashboard Widgets

How can I achieve this?

Thanks.


Solution

  • First, I have revisited your code as you where using 2 times the same hooks. So know you have 2 hooked functions instead of 4.

    To answer to your question: YES there is a working admin hook that I have just tested that will include orders with your custom statuses in the WooCommerce Admin Dashboard Stats widget: woocommerce_reports_get_order_report_data_args hook.

    Here is this code:

    // Register new status
    function register_custom_order_statuses() {
        register_post_status('wc-awaiting-shipment', array(
            'label' => 'Awaiting Shipment',
            'public' => true,
            'exclude_from_search' => false,
            'show_in_admin_all_list' => true,
            'show_in_admin_status_list' => true,
            'label_count' => _n_noop('Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>')
        ));
    
        register_post_status('wc-dispatched', array(
            'label' => 'Dispatched',
            'public' => true,
            'exclude_from_search' => false,
            'show_in_admin_all_list' => true,
            'show_in_admin_status_list' => true,
            'label_count' => _n_noop('Dispatched <span class="count">(%s)</span>', 'Dispatched <span class="count">(%s)</span>')
        ));
    }
    add_action('init', 'register_custom_order_statuses');
    
    
    // Add to list of WC Order statuses
    function add_custom_order_statuses($order_statuses) {
        $new_order_statuses = array();
    
        // add new order status after processing
        foreach ($order_statuses as $key => $status) {
            $new_order_statuses[$key] = $status;
            if ('wc-processing' === $key) {
                $new_order_statuses['wc-awaiting-shipment'] = 'Awaiting shipment';
                $new_order_statuses['wc-dispatched'] = 'Dispatched';
            }
        }
        return $new_order_statuses;
    }
    add_filter('wc_order_statuses', 'add_custom_order_statuses');
    
    
    // Admin reports for custom order status
    function wc_reports_get_order_custom_report_data_args( $args ) {
        $args['order_status'] = array( 'completed', 'processing', 'on-hold', 'awaiting-shipment', 'dispatched' );
        return $args;
    };
    add_filter( 'woocommerce_reports_get_order_report_data_args', 'wc_reports_get_order_custom_report_data_args');
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The code is tested and fully functional.


    References: