问题
In WooCommerce, I have made 2 custom order statuses. The first one is 'shipping status' and the other one is 'approved status'.
After I have changed some orders status to these two new statuses (which are shipping
or approved
), the customer can't view this orders on his order history page.
Here is my code:
function register_awaiting_shipment_order_status() {
if('product_manager' == $get_roles || 'administrator' == $get_roles){
register_post_status( 'wc-shipping', array(
'label' => 'wc-shipping',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Shipping <span class="count">(%s)</span>', 'Shipping <span class="count">(%s)</span>' )
) );
}
if('approver' == $get_roles || 'administrator' == $get_roles ||'product_manager' == $get_roles ){
register_post_status( 'wc-approved', array(
'label' => 'wc-approved',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Approved <span class="count">(%s)</span>', 'Approved <span class="count">(%s)</span>' )
) );
}
}
add_action( 'init', 'register_awaiting_shipment_order_status' );
But if I change the order status back to the Woocommerce's
default order statuses (for example "completed"), then the customer can view them i his order history again.
What I am doing wrong?
How can I display new orders with that new custom statuses on customer order history page?
Thanks.
回答1:
I have corrected small mistakes in your code. You will need to replace textdomain
everywhere by your theme text domain (or slug).
To complete and display this new custom orders status, you need also to register them in
wc_order_statuses
filter hook that is used onmy_account/orders.php
template that displays customers orders on their account pages.
This is the revisited code:
function register_new_custom_order_statuses() {
if('product_manager' == $get_roles || 'administrator' == $get_roles){
register_post_status( 'wc-shipping', array(
'label' => _x( 'Shipping', 'Order status', 'textdomain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Shipping <span class="count">(%s)</span>', 'Shipping <span class="count">(%s)</span>' )
) );
}
if('approver' == $get_roles || 'administrator' == $get_roles ||'product_manager' == $get_roles ){
register_post_status( 'wc-approved', array(
'label' => _x( 'Approved', 'Order status', 'textdomain' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Approved <span class="count">(%s)</span>', 'Approved <span class="count">(%s)</span>' )
) );
}
}
add_action( 'init', 'register_new_custom_order_statuses' );
// Register new statuses in wc_order_statuses (function).
function register_new_custom_wc_order_statuses( $order_statuses ) {
$order_statuses['wc-shipping'] = _x( 'Shipping', 'Order status', 'textdomain' );
$order_statuses['wc-approved'] = _x( 'Approved', 'Order status', 'textdomain' );
return $order_statuses;
}
add_filter( 'wc_order_statuses', 'register_new_custom_wc_order_statuses' );
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:
- WooCommerce Apidocs source Function wc_get_order_statuses()
- WooCommerce - Registering new order statuses
来源:https://stackoverflow.com/questions/39693958/custom-order-status-arent-displayed-on-the-customer-my-account-order-history