John Davidson

php - Include order count from custom order status in WooCommerce admin menu

0 comments
Message:


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



My Answer:
> 'Produktionsklar',
> 'public' => true,
> 'exclude_from_search' => false,
> 'show_in_admin_all_list' => true,
> 'show_in_admin_status_list' => true,
> 'label_count' => _n_noop( 'Produktionsklar (%s)', 'Produktionsklar (%s)' )
);
}
add_action( 'init', 'register_produktionsklar_order_status' );

This code registers a new order status called "Produktionsklar" in WooCommerce.

To include the order count for this custom order status in the WooCommerce admin menu, you can use the following code:

php
function add_produktionsklar_order_count_to_menu() {
global $menu;

$order_count = wp_count_posts( 'shop_order' );
$produktionsklar_count = isset( $order_count->wc-produktionsklar ) ? $order_count->wc-produktionsklar : 0;

foreach ( $menu as $key => $value ) {
if ( $value[0] == 'WooCommerce' ) {
$menu[$key][0] .= ' ' . $produktionsklar_count . '';
return;
}
}
}
add_action( 'admin_menu', 'add_produktionsklar_order_count_to_menu' );


This code retrieves the count of orders with the custom status "Produktionsklar" and adds it to the WooCommerce admin menu next to the WooCommerce menu item.

Rate this post

4 of 5 based on 2265 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us