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

标签 php wordpress woocommerce dashboard orders

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

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

由于 waiting shipmentdispatched 是自定义订单状态,WooCommerce 统计小部件不反射(reflect)这些订单销售总额。问题是我有很多订单都处于 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');

Woocommerce Status Dashboard Widgets

我怎样才能做到这一点?

谢谢。

最佳答案

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

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


引用资料:

关于php - 在 Admin Dashboard Stats Widget 中添加自定义订单状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39716254/

相关文章:

php - PHP 中的 GOTO 命令?

php - 如何使用 AJAX、JSON 和 PHP 在选择框中更改值来检索表单?

PHP 正则表达式匹配所有 url

php - 在 Woocommerce 购物车和结帐项目中显示自定义字段的值

php - 将自定义批量操作添加到 Woocommerce 3 中的管理员订单列表

php - 使用 Smarty 修复响应式 Bootstrap 列

css - 强制 Logo 与菜单保持一致

php - 在 Woocommerce 中的简短描述后移动产品描述

php - Wordpress 无法追踪的迁移死亡白屏

wordpress - WooCommerce - 添加对象术语无法正常工作