Searching WooCommerce Orders

Last updated December 18, 2018 « Knowledge Base

Note: SearchWP 2.8.4+ required

Searching WooCommerce Order data can be greatly improved by having SearchWP take care of the heavy lifting. By default, WooCommerce excludes shop_order from search results, but we can tell SearchWP that we do in fact want it to index Order data by following these instructions.

Step 1: Enable Admin Searches

You will need to enable SearchWP in the WP admin by ticking the appropriate checkbox on the Advanced tab of the SearchWP settings screen:

Enable SearchWP in the WordPress admin area

Once this checkbox has been ticked, SearchWP will take over searching in the WordPress administration area (Dashboard)

Step 2: Allow SearchWP to index Orders

NOTE: WooCommerce Order data is sensitive information and proper precautions should be taken to prevent unwanted exposure. Perform your own tests to ensure Orders are not showing up in your public-facing searches.

<?php
/**
* Tell SearchWP that it can index WooCommerce Orders in the WP admin
*/
function my_searchwp_indexed_post_types( $post_types ) {
if ( ! is_array( $post_types ) ) {
$post_types = (array) $post_types;
}
if ( ! in_array( 'shop_order', $post_types ) ) {
$post_types[] = 'shop_order';
}
return $post_types;
}
add_filter( 'searchwp_indexed_post_types', 'my_searchwp_indexed_post_types' );
/**
* Add WooCommerce Order post types to SearchWP's considered post status array when in the WP admin
*/
function my_searchwp_post_statuses( $post_statuses, $engine ) {
if ( ! is_array( $post_statuses ) ) {
$post_statuses = (array) $post_statuses;
}
$post_statuses[] = 'wc-pending';
$post_statuses[] = 'wc-processing';
$post_statuses[] = 'wc-on-hold';
$post_statuses[] = 'wc-completed';
$post_statuses[] = 'wc-cancelled';
$post_statuses[] = 'wc-refunded';
$post_statuses[] = 'wc-failed';
return $post_statuses;
}
add_filter( 'searchwp_post_statuses', 'my_searchwp_post_statuses', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

These snippets tell SearchWP to do the following:

  1. Allow indexing of WooCommerce Orders
  2. Allow indexing of WooCommerce Order post statuses

Step 3: Create an Orders Supplemental Engine

Create a Supplemental Engine and Add the Orders post type along with any other settings you’d like:

Add an Orders Supplemental Engine

Note that an Any Custom Field has been added to the post type as well. This will ensure all of the Order metadata (including address information) is searchable.

Once the engine has been created, save your settings.

Step 4: Use the Orders engine when searching WooCommerce Orders

The last step is to add another hook to your theme’s functions.php that tells SearchWP to use the orders Supplemental Engine when performing searches of WooCommerce Orders in the WordPress administration area:

<?php
/**
* When performing an admin search for WooCommerce Orders, use the 'Orders' SearchWP engine
*/
function my_searchwp_search_args( $args ) {
$search_in_admin = apply_filters( 'searchwp_in_admin', false );
if ( wp_doing_ajax() || empty( $_GET['post_type'] ) || ! is_admin() || empty( $search_in_admin ) ) {
return $args;
}
if ( 'shop_order' !== $_GET['post_type'] ) {
return $args;
}
// WooCommerce adds some limiting that we're going to get rid of here
add_filter( 'searchwp_include', function( $ids ){
return is_admin() && is_search() ? array() : $ids;
}, 999 );
$args['engine'] = 'orders';
return $args;
}
add_filter( 'searchwp_search_args', 'my_searchwp_search_args' );
view raw functions.php hosted with ❤ by GitHub

NOTE: Line 17 contains the name (not the label) of the Supplemental Engine we created in Step 3

With that hook in place you can effectively search WooCommerce Orders using SearchWP!

Optionally include additional Order data

You can tell SearchWP to index any other data along with your Orders. For example:

WooCommerce uses WordPress Post IDs as Order numbers, which SearchWP doesn’t index by default. If you plan on searching your Orders by Order Number, you can add this snippet to your  theme’s functions.php:

<?php
/**
* Tell SearchWP's indexer to index WooCommerce Order Numbers as extra metadata
*
* @param $extra_meta
* @param $post_being_indexed
*
* @return mixed
*/
function my_searchwp_index_wc_order_number( $extra_meta, $post_being_indexed ) {
if ( 'shop_order' !== get_post_type( $post_being_indexed ) ) {
return $extra_meta;
}
// Index plain order number and these variants:
// order-{order_number}
// o-{order_number}
$extra_meta['searchwp_order_number'] = $post_being_indexed->ID . ' order-' . $post_being_indexed->ID . ' o-' . $post_being_indexed->ID;
return $extra_meta;
}
add_filter( 'searchwp_extra_metadata', 'my_searchwp_index_wc_order_number', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

Note: in order to search 1 or 2 digit Order Numbers you will also need to disable the minimum word length by ticking the appropriate checkbox on the Advanced tab of the SearchWP settings screen.

You can follow this process to index any other data as “extra” metadata (make sure you have ‘Any Custom Field’ selected for your Orders post type in order to include any extra metadata in your searches). For more information please see the docs for searchwp_extra_metadata.

Making Order Notes searchable

WooCommerce also imposes some restrictions on Order Notes which prevents them from being searchable by default. If you would like to include Order Notes in SearchWP-powered searches of WooCommerce Orders, you will need to first ensure that Comments have a weight in your SearchWP engine configuration (see screenshot above).

You will also need to utilize SearchWP’s searchwp_indexer_pre_get_comments hook to unhook WooCommerce’s restriction on Order Notes. You can do that by adding the following to a custom plugin or your active theme’s functions.php:

<?php
/**
* Let SearchWP index WooCommerce Order Notes
*/
function my_searchwp_indexer_pre_get_comments() {
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
}
add_action( 'searchwp_indexer_pre_get_comments', 'my_searchwp_indexer_pre_get_comments' );
view raw functions.php hosted with ❤ by GitHub

Once that’s in place, your Order Notes will also be indexed by SearchWP and made searchable.

Fix Search on Your Site. No Coding Required!

Now you can utilize all of the content that's gone unrecognized by native WordPress search instantly with SearchWP

Get SearchWP