WooCommerce: Search Orders By 2-Letter Country Code [HPOS]

WooCommerce’s order search function is essential for store admins, but with the introduction of High-Performance Order Storage (HPOS), searching by billing or shipping country code may no longer work as expected.

If you’re used to quickly filtering orders by country, this limitation can be frustrating. Fortunately, there’s a simple way to bring back this functionality using a custom code snippet.

In this post, we’ll walk through how to modify the WooCommerce order search to support two-letter country codes, making it easier to find the orders you need. Let’s dive into the solution!

My goal is to add a new option to the HPOS “Search orders” dropdown in the WordPress Admin, so that we can quickly filter orders by billing/shipping country.

PHP Snippet: Enable Order Search by 2-Letter Country Code @ WordPress Admin [HPOS]

This code consists of two main parts.

The first part adds a custom filter to WooCommerce’s High-Performance Order Storage (HPOS) search options, allowing an admin to filter orders by a two-letter country code.

The second part hooks into the query generation to modify the WHERE clause, enabling the search to match the entered country code with the country stored in the order addresses. Specifically, it checks if the search filter is set to “country” and, if so, adjusts the query to search for orders where the billing or shipping address matches the provided two-letter country code. This is achieved by joining the orders table with the order addresses table and filtering on the country field.

/**
 * @snippet       Search Orders By Country Code
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_hpos_admin_search_filters', function( $options ) {
	$options['country'] = '2-Letter Country Code';
	return $options;
});

add_filter( 'woocommerce_hpos_generate_where_for_search_filter', 'filter_orders_by_country', 10, 4 );

function filter_orders_by_country( $where, $search_term, $search_filter, $query ) {
    global $wpdb;
	 if ( 'country' === $search_filter ) {
		$orders = $wpdb->prepare( "{$wpdb->prefix}wc_orders.id IN ( SELECT order_id FROM {$wpdb->prefix}wc_order_addresses WHERE country = %s )", $search_term );
		return $orders;
    }
    return $where;
}

And here’s a screenshot showing the results when searching for “IT”:

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *