Adding custom order statuses in Admin Dashboard Stats Widget

你。 提交于 2019-12-05 18:12:29
LoicTheAztec

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!