SearchWP

This Documentation is for SearchWP Version 3

Searching WooCommerce Orders

Note: This documentation is for SearchWP 3.x
SearchWP 4: Search WooCommerce Orders

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: Make Orders Searchable

By default WooCommerce Orders are not searchable, but with a couple of hooks we can make sure SearchWP can work with them as it can with searchable post types.

Note: Orders contain information you (very) likely do not want to expose publicly. These snippets take that position into account by only making the data available for the indexer and when performing a search on Orders. Please take the proper steps to ensure that your Order data is appropriately protected!

To make Orders (and their custom post statuses) searchable, add the following to your theme’s functions.php or a custom plugin:

<?php
/**
* Tell SearchWP that it can index WooCommerce Orders.
*/
add_filter( 'searchwp_indexed_post_types', function( $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;
} );
/**
* Tell SearchWP to index all WooCommerce Order post statuses.
*/
add_filter( 'searchwp_post_statuses', function( $post_statuses, $engine ) {
if ( ! is_array( $post_statuses ) ) {
$post_statuses = (array) $post_statuses;
}
return array_unique( array_merge( $post_statuses, array(
'wc-pending',
'wc-processing',
'wc-on-hold',
'wc-completed',
'wc-cancelled',
'wc-refunded',
'wc-failed',
) ) );
}, 10, 2 );
view raw functions.php hosted with ❤ by GitHub

Once that snippet is added, you’ll be able to work with Orders in SearchWP.

Step 2: Create an Orders search engine

With Orders available to search, create a new Supplemental Engine on the main tab of SearchWP’s settings screen that includes only Orders any any Custom Fields or Taxonomies you want:

Note: This article assumes your engine label is Orders so the name will be orders in code snippets.

Animated GIF of adding an Orders supplemental search engine

Finish setting up the engine by clicking the Save Engines button and the indexer will index your Orders.

Step 3: Utilize the Orders engine

With the Orders supplemental engine in place, the last step is to tell SearchWP to use that engine for searches in the admin. This can be done on the Advanced tab of the SearchWP settings screen:

Screenshot of Admin search engine on the SearchWP settings screen Advanced tab

Once that setting is in place you’ll be able to easily search all of your WooCommerce Orders!

Make additional Order data searchable (optional)

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 engine setup 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 too!

[wpforms id="3080"]