I have added a custom order status using this code in functions.php
// Register new order status
function register_produktionsklar_order_status() {
register_post_status( 'wc-produktionsklar', array(
'label' => 'Klar til produktion',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Klar til produktion (%s)', 'Klar til produktion (%s)' )
) );
}
add_action( 'init', 'register_produktionsklar_order_status' );
// Add new order status to list of WC Order statuses
function add_produktionsklar_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-afventer-godkend' === $key ) {
$new_order_statuses['wc-produktionsklar'] = 'Klar til produktion';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_produktionsklar_to_order_statuses' );
I would like to include this custom order status in the order count. When I change the status to this custom status, the order is no longer counted in the open orders number. (see attached image for clarity)
Any help would be great
By default only orders with the status 'processing' are counted and added to the orders count number.
To adjust this we are going to use the woocommerce_menu_order_count
filter hook and wc_orders_count()
function, which return the orders count of a specific order status.
So you get:
/**
* Add orders count of a specific order status
*/
function filter_woocommerce_menu_order_count( $wc_processing_order_count ) {
// Call function and pass custom order status
$order_count_produktionsklar = wc_orders_count( 'produktionsklar' );
return $wc_processing_order_count + $order_count_produktionsklar;
}
add_filter( 'woocommerce_menu_order_count', 'filter_woocommerce_menu_order_count', 10, 1 );
I also have rewritten your code with this updated code. For example, the init
hook has been replaced with woocommerce_register_shop_order_post_statuses
to register custom order statuses.
/**
* Register order status
*/
function filter_woocommerce_register_shop_order_post_statuses( $order_statuses ) {
// Status must start with "wc-"
$order_statuses['wc-produktionsklar'] = array(
'label' => _x( 'Klar til produktion', 'Order status', 'woocommerce' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: number of orders */
'label_count' => _n_noop( 'Klar til produktion <span class="count">(%s)</span>', 'Klar til produktion <span class="count">(%s)</span>', 'woocommerce' ),
);
return $order_statuses;
}
add_filter( 'woocommerce_register_shop_order_post_statuses', 'filter_woocommerce_register_shop_order_post_statuses', 10, 1 );
/**
* Show order status in the dropdown @ single order
*/
function filter_wc_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 ) {
// Status must start with "wc-"
$new_order_statuses['wc-produktionsklar'] = _x( 'Klar til produktion', 'Order status', 'woocommerce' );
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'filter_wc_order_statuses', 10, 1 );
/**
* Show order status in the dropdown @ bulk actions
*/
function filter_bulk_actions_edit_shop_order( $bulk_actions ) {
// Note: "mark_" must be there instead of "wc"
$bulk_actions['mark_produktionsklar'] = __( 'Klar til produktion', 'woocommerce' );
return $bulk_actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_bulk_actions_edit_shop_order', 10, 1 );