Skip to content

Limit WooCommerce Order Search to Order Numbers Only

WooCommerce has a helpful search mechanism that you can use to locate orders in the admin. It can find orders by the user’s name, address, email, or order number. We can find this in woocommerce/includes/data-stores/class-wc-order-data-store-cpt.php starting on line 521.

$search_fields = array_map(
	'wc_clean',
	apply_filters(
		'woocommerce_shop_order_search_fields',
		array(
			'_billing_address_index',
			'_shipping_address_index',
			'_billing_last_name',
			'_billing_email',
		)
	)
);
$order_ids     = array();

What this means is WooCommerce will search though all of this extra information to find an order. While that can be helpful, it may take a long time if the site has an extremely large number of orders.

Notice that in the filters, the order number search isn’t mentioned. What we can do is create a function that removes these search options so that only order numbers are searched. Here’s a snippet that can do that for you.

add_filter( 'woocommerce_shop_order_search_fields', '__return_false' );

Now when I search in the order admin, I can still search for orders by order number, but with the other criteria removed, searching is much faster.

Search by order number
Search by last name

Here’s what a typical search by last name looks like:

Normal search by last name

We can also add additional search criteria with this filter. Say we wanted to be able to search by the order total. To do that we need to add _order_total to the filter. Here’s one way to do that.

function ijab_woo_extra_order_meta_search($args) {
    
	$args[] = '_order_total';
	
	return $args;
}
add_filter( 'woocommerce_shop_order_search_fields', 'ijab_woo_extra_order_meta_search' );

With that you can search by the order total amount and have it show matching orders.

So now you know how to modify the built-in order search for WooCommerce. If you have any other creative uses for this or questions, let me know in the comments.

Leave a Reply

%d bloggers like this: