在 Admin Dashboard Stats 小部件中添加自定义订单状态
Posted
技术标签:
【中文标题】在 Admin Dashboard Stats 小部件中添加自定义订单状态【英文标题】:Adding custom order statuses in Admin Dashboard Stats Widget 【发布时间】:2017-02-04 14:08:23 【问题描述】:我想在 WooCommerce Admin Dashboard Stats 小部件中包含来自自定义订单状态的详细信息。我设置了wc-processing
之后的2个自定义订单状态。
支付成功后的订单流程为:
wc-processing
=>wc-awaiting-shipment
=>wc-dispatched
=>wc-completed
.
由于 awaiting shipment
和 dispatched
是自定义订单状态,WooCommerce 统计小部件不会反映这些订单的总销售额。问题是我有很多 wc-dispatched
和 wc-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');
我怎样才能做到这一点?
谢谢。
【问题讨论】:
@LoicTheAztec:我现在正在旅行,所以现在无法测试,8 小时后可以更新。很抱歉没有通知。 【参考方案1】:首先,我重新审视了您的代码,因为您使用了 2 次相同的钩子。所以知道你有 2 个挂钩函数而不是 4 个。
回答您的问题:是的,我刚刚测试了一个有效的管理挂钩,它将包含您在 WooCommerce 管理仪表板统计小部件中的自定义状态的订单:
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 文件中或任何插件文件中。
代码已经过测试并且功能齐全。
参考资料:
WooCommerce source class WC_Admin_Report - get_order_report_data() Custom order status aren't displayed on the customer my account order history【讨论】:
嗨 @LoicTheAztec 我刚刚看到woocommerce_reports_get_order_report_data_args
过滤器完美地计算订单总数,但在 WooCommerce wc-reports 页面中显示 0 个项目。我注释掉最后一行(即add_filter
)项目计数显示正确。如何解决这个问题?请检查此screenshot。
@RaunakGupta 我试图找出一些让它起作用的技巧......但我暂时没有得到它。
有没有办法为自定义订单状态的总计框(向下第 3 行 - 当前显示等待处理和搁置)添加新框?
嗨@LoicTheAztec:你有什么可行的解决方案吗(我在screenshot中给出的解决方案)?我试图通过搜索它的核心文件来找到解决方案,但没有成功。
嗨@RaunakGupta ...不,我没有搜索。解决这个问题的方法似乎很长而且很复杂,如果有的话……正如有人所说,“思考不同”可能会启发你。以上是关于在 Admin Dashboard Stats 小部件中添加自定义订单状态的主要内容,如果未能解决你的问题,请参考以下文章
Starting proxy admin_stats: cannot bind socket [192.168.144.249:9046]
Custom models display ordering for Django admin dashboard
bootstrap——free bootstrap admin dashboard templates