我想在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');
我怎样才能做到这一点?
感谢。
答案 0 :(得分:5)
首先,我重新访问了您使用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文件中或任何插件文件中。
代码已经过测试并且功能齐全。
参考文献: