2

I need to hide orders with a specific status in the WooCommerce admin orders list. (wp-admin/edit.php?post_type=shop_order).

CSS won't work as if the user only shows 20 rows, there might be a page with no results as many orders might have this status.

I tried the following function:

add_action('wc_order_statuses', 'my_statuses');

function my_statuses($order_statuses) {

    unset($order_statuses['wc-my_status']);
    return $order_statuses;
}

...but that made conflict with my function (below) on the Thank you page which no longer changed the order status to my custom status, probably because the function mentioned above removes it.

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){

    if( // my custom code ) {
        $order->update_status( 'my_status' );
      }
}

Is there no easy way to hide orders with my_status from the order list in WooCommerce admin panel?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
BTB
  • 352
  • 4
  • 19

1 Answers1

3

To hide row(s) containing a specific order status, in WooCommerce admin order list. You can use the parse_query action hook.

So you get:

function action_parse_query( $query ) { 
    global $pagenow;
    
    // Your order status to hide, must start with 'wc-'
    $hide_order_status = 'wc-completed';

    // Initialize
    $query_vars = &$query->query_vars;
    
    // Only on WooCommerce admin order list
    if ( $pagenow == 'edit.php' && $query_vars['post_type'] == 'shop_order' ) {
        // Finds whether a variable is an array
        if ( is_array( $query_vars['post_status'] ) ) {  
            // Searches the array for a given value and returns the first corresponding key if successful
            if ( ( $key = array_search( $hide_order_status, $query_vars['post_status'] ) ) !== false ) {
                unset( $query_vars['post_status'][$key] );
            }
        }
    }

}
add_action( 'parse_query', 'action_parse_query', 10, 1 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50