Search code examples
phpwordpresswoocommercebackendorders

Add custom order status count to admin WooCommerce menu


Based on that article Include order count from custom order status in WooCommerce admin menu, I know that we can adjust the count from the WooCommerce default "in processing" orders count. We want to add a second count for orders in a special status "shipped". I don't find a way to add this second count in that area.

// 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_shipped = wc_orders_count( 'shipped' );
    
    return $wc_processing_order_count + $order_count_shipped;
}
add_filter( 'woocommerce_menu_order_count', 'filter_woocommerce_menu_order_count', 10, 1 );

Does someone know how I can add there such a count in blue?

enter image description here


Solution

  • This can be done easily with the following function, based on menu_order_count() WooCommerce core function hooked in 'admin_menu' action hook.

    The code that will add a 2nd count (in blue) for "shipped" (custom status) orders:

    add_action( 'admin_menu', 'menu_order_count2', 20  );
    function menu_order_count2() {
        global $submenu;
    
        if ( isset( $submenu['woocommerce'] ) ) {
            // Add count if user has access.
            if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'edit_others_shop_orders' ) ) {
                $custom_status = 'completed'; // <= Here set your custom status
                $order_count = wc_orders_count( $custom_status );
    
                if ( $order_count ) {
                    foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
                        if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {
                            $submenu['woocommerce'][ $key ][0] .= sprintf(
                                ' <span class="%s" style="%s"><span class="%s-count">%s</span></span>',
                                'awaiting-mod update-plugins count-'. esc_attr( $order_count ),
                                'background-color:#007fe5', $custom_status, number_format_i18n( $order_count )
                            );
                            break;
                        }
                    }
                }
            }
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    You will get:

    enter image description here