How to Filter WooCommerce Order Table

This how-to is a follow up to the How to Hide Zero and Free Orders post. This is a developer focused how-to, where we will dig into some WooCommerce code.

This how-to will discuss the WooCommerce Hide Zero Orders plugin hosted on GitHub here. You’ll want to grab a copy or clone it locally to review the code.

The plugin is very simple and consists of one file woocommerce-hide-zero-orders.php.

The first part of the plugin is the standard plugin header. There are two functions wrapped in a call to is_admin() so that the code is only executed in the WordPress dashboard.

Action for restrict_manage_posts

The first action hooks into ‘resticct_manage_posts‘ and will only work on posts of type ‘shop_order‘.

If the post is a shop_order type, then the code displays a dropdown with two options: Show all orders, or Show non-zero orders. The currently selected option is set by a post variable named ‘sp_order_view‘.

Action for filtering request

The second action is the piece of code that does the query filtering. The request filter can be used to modify the variables that are passed to SQL queries. The only parameter is $vars, which is an array of query vars.

Our hooked function first checks if the post type is shop_order and the sp_order_view variable is set. This lets us know that the request is from the WooCommerce Orders page.

If both options are true and the dropdown is set to “Show non-zero orders,” then we add a query for the IDs of orders with a post meta key of _order_total that has a value of 0.00. This is placed in the key ‘post__not_in‘ so that only orders not in the sub query will be displayed.

A possible issue

The posts__not_in query uses ‘posts_per_page’ of -1 which can cause performance issues on sites with large numbers of orders.

If you have any questions on this how-to or how to extend your WooCommerce store, please get in touch and we’d be happy to chat!