在 Admin Dashboard Stats Widget 中添加自定义订单状态

Adding custom order statuses in Admin Dashboard Stats Widget

我想在 WooCommerce 管理仪表板统计小部件中包含自定义订单状态的详细信息。我已经设置了 wc-processing.

之后的 2 个自定义订单状态

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

由于 awaiting shipmentdispatched 是自定义订单状态,WooCommerce 统计小部件不反映这些订单在总销售额中。问题是我有很多订单 wc-dispatchedwc-awaiting-shipment 状态。

这是我用来注册此自定义订单状态的代码:

/**
 * 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');

我怎样才能做到这一点?

谢谢。

首先,我已经像您一样重新访问了您的代码,其中使用了 2 次相同的钩子。所以知道你有 2 个挂钩函数而不是 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.

这是这段代码:

// 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');

此代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

代码已经过测试并且功能齐全。


参考文献: