slimpavlil
slimpavlil

Reputation: 13

How to add count in admin menu of a custom order status in Woocommerce?

So I've added a custom order status called "awaiting-shippment" and I want to include these orders in the order count displayed under the WooCommerce dropdown menu on the left of the administration. The only orders being displayed here are the orders with the "processing" status.

Any help, folks? Thanks alot in advance.

Upvotes: 1

Views: 1377

Answers (3)

Pial Arifur Rahman
Pial Arifur Rahman

Reputation: 1

Here is the solution

function add_order_status_count(){
    global $submenu;
    $order_count = wc_orders_count( 'awaiting-shippment' );
    foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
        if ( 0 === strpos( $menu_item[0], 'Orders' ) ) {
            $submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // WPCS: override ok.
            break;
        }
    }
}
add_action( 'admin_head', 'add_order_status_count');

And if you want to disable Woocommerce processing order status count

function remove_processing_order_count() {
    return false;
}
add_filter( 'woocommerce_include_processing_order_count_in_menu', 'remove_processing_order_count' );

Upvotes: 0

userlond
userlond

Reputation: 3824

If you want completely replace default counter, the snippet bellow should fit your needs:

<?php
add_action( 'admin_head', 'usrlnd_action_change_order_counter' ), 999 );
function usrlnd_action_change_order_counter() {
    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( 'manage_woocommerce' ) ) {
            // my custom status slug is `new`
            $order_count     = wc_orders_count( 'new' );
            $old_order_count = wc_processing_order_count();

            if ( $order_count ) {
                foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
                    if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) {
                        $old  = ' <span class="awaiting-mod update-plugins count-' . esc_attr( $old_order_count ) . '"><span class="processing-count">' . number_format_i18n( $old_order_count ) . '</span></span>';
                        $new  = ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>';
                        $item = $submenu['woocommerce'][ $key ][0];
                        if ( strpos( $item, $old ) !== false ) {
                            // replace old counter with new
                            $item = str_replace( $old, $new, $item );
                        } else {
                            // there is no counter, just add the new
                            $item .= $new;
                        }

                        $submenu['woocommerce'][ $key ][0] = $item;
                        break;
                    }
                }
            }
        }
    }
}

Tested with Woocommerce 4.2.2.

Upvotes: 0

Shir Gans
Shir Gans

Reputation: 2027

I've found a fair solution. It's a bit hacky... but it works

First, add the submenu to WooCommerce menu:

add_action( 'admin_menu', 'custom_status_menu', 50 );
function custom_status_menu(){
    $status_page = add_submenu_page( 'woocommerce', 'Awaiting Shipment', __( 'Awaiting', 'woocommerce' ), 'manage_woocommerce', 'wc-awaiting', 'awaiting_orders_page' );
}

Then, add a function to run when you click this menu item. This function (and here is the hacky part) will redirect the user to the regular orders page, just showing the requested status. (for testing I used "Canceled" status, you should change that)

function awaiting_orders_page(){
    header('Location: /wp-admin/edit.php?post_status=wc-cancelled&post_type=shop_order');
}

And lastly, add the counter. Same here, I used "awaiting" status, change it to the one your created

add_action( 'admin_head', 'add_custom_status_count');
function add_custom_status_count(){
    global $submenu;
    $order_count = wc_orders_count( 'awaiting' );
    foreach ( $submenu['woocommerce'] as $key => $menu_item ) {
        if ( 0 === strpos( $menu_item[0], 'Awaiting' ) ) {
            $submenu['woocommerce'][ $key ][0] .= ' <span class="awaiting-mod update-plugins count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // WPCS: override ok.
            break;
        }
    }
}

Upvotes: 1

Related Questions