Note: You are viewing the documentation for Version 4View Version 3 Docs

Search WooCommerce Orders

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

WooCommerce Orders contain a lot of information and are sometimes difficult to search effectively. SearchWP can be set up to search WooCommerce Orders quickly.

Step 1: Make WooCommerce Orders searchable

By default SearchWP will not list WooCommerce Orders as a Source because that particular content type is not public by design. WooCommerce Orders contain sensitive information and extra care must be taken when making changes like these!

Use the following hooks to add WooCommerce Orders as a SearchWP Source:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Add WooCommerce Orders as a SearchWP Source.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
add_action( 'plugins_loaded', function() {
add_filter( 'searchwp\sources', function( $sources ) {
$sources[] = new \SearchWP\Sources\Post( 'shop_order' );
return $sources;
} );
add_filter( 'searchwp\post_stati\shop_order', function( $post_stati ) {
return array_merge( $post_stati, [
'wc-pending',
'wc-processing',
'wc-on-hold',
'wc-completed',
'wc-cancelled',
'wc-refunded',
'wc-failed',
] );
} );
} );

Step 2: Create a supplemental Engine for Admin searches

Next, create a supplemental SearchWP Engine to use for Admin searches:

Animated GIF creating a new supplemental Engine

Note: This Engine will be used for all searches in the Admin because that checkbox was ticked in the setup. You can omit any Source(s) from this Engine and SearchWP will skip Admin searches for those Sources, falling back to WordPress’ default.

Here are the steps that were taken to create this Admin search Engine:

  1. Click the Add New button on the Engines tab of the SearchWP settings screen
  2. Click the Sources & Settings button for the newly created Engine
  3. Enable the applicable Sources for this Engine (making sure to include Orders)
  4. Important: Tick the Admin Engine checkbox
  5. Click the Save Engines button to save the new Engine

Search WooCommerce Orders

Once the index has processed your existing WooCommerce Orders, they will now be searchable in the WordPress Admin!

Note: There are some aspects of WooCommerce Orders that may need further integration:

Making Order Numbers searchable

Order numbers are Post IDs which are not indexed by default. We can tell SearchWP to index WooCommerce Order Numbers alongside the other Order data with a snippet like this:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Add WooCommerce Order Numbers to WooCommerce Orders.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
$my_extra_meta_key = 'searchwp_order_number';
$my_extra_meta = $entry->get_id() . ' order-' . $entry->get_id() . ' o-' . $entry->get_id();
$data['meta'][ $my_extra_meta_key ] = $my_extra_meta;
return $data;
}, 20, 2 );

Because we added ‘Any Meta Key’ to the Admin Engine we created, we’ll now be able to search by Order number as well.

Making Order Notes searchable

If you’ll recall we added ‘Comments’ to the Admin Engine above. WooCommerce uses Comments for Order Notes, but there is an additional restriction put in place by WooCommerce due to the private nature of Order Notes. We can remove this restriction for SearchWP with a snippet like this:

All hooks should be added to your custom SearchWP Customizations Plugin.

<?php
// Make WooCommerce Order Notes searchable by SearchWP.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
add_action( 'searchwp\source\post\attributes\comments', function() {
if ( did_action( 'searchwp\indexer\batch' ) ) {
remove_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] );
}
} );

Once this snippet has been added you’ll need to rebuild your index by clicking the Rebuild Index button on the Engines tab of the SearchWP settings screen, and when the index has been rebuilt your WooCommerce Order Notes will be searchable as well.

Fix keyword search on your site. No coding required!

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

Get SearchWP