SearchWP

Version 4 Documentation

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.

Note: You will need SearchWP’s WooCommerce Integration Extension active for this process.

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() {
if ( class_exists( 'WooCommerce' ) ) {
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:

SearchWP WooCommerce orders engine setup

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 Item data searchable

If you’d like to make Order Item data (e.g. Product SKU numbers for Order Items) you can customize the data indexed for each Order to include any Order Item data you’d like. In this snippet we’ll make the SKU, Product Name, Product Slug, Product Description, Product Short Description, and Product Attributes searchable to return any Orders that match:

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

<?php
// Add WooCommerce Order item data to WooCommerce Orders Search in SearchWP.
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// The Product data keys to index for each Order item.
$data_to_index = [
'sku',
'name',
'slug',
'description',
'short_description',
'attributes',
];
if (
'post' . SEARCHWP_SEPARATOR . 'shop_order' !== $entry->get_source()->get_name()
|| ! function_exists( 'wc_get_order' )
|| ! class_exists( 'WC_Order_Item_Product' )
|| ! method_exists( 'WC_Order_Item_Product', 'get_product' )
|| ! class_exists( 'WC_Product' )
|| ! method_exists( 'WC_Product', 'get_data' )
) {
return $data;
}
$order = wc_get_order( $entry->get_id() );
$data['meta']['searchwp_wc_order_item_data'] = array_map( function ( $item ) use ( $data_to_index ) {
$product = $item->get_product();
$product_data = $product->get_data();
return array_filter( array_map( function( $item_key, $item_value ) use ( $data_to_index ) {
return in_array( $item_key, $data_to_index ) ? $item_value : '';
}, array_keys( $product_data ), array_values( $product_data ) ) );
}, $order->get_items() );
// We need to prevent WooCommerce from excluding the order comments from our query.
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
$order_comments = get_comments( array(
'post_id' => $order->get_id(),
'orderby' => 'comment_ID',
'order' => 'DESC',
'status' => 'any',
'type' => 'order_note',
) );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
$notes = wp_list_pluck( $order_comments, 'comment_content' );
$data['meta']['searchwp_wc_order_notes'] = $notes;
return $data;
}, 20, 2 );
// Prevent WooCommerce from limiting the results to post IDs.
add_filter( 'searchwp\native\args', function( $args, $query ) {
if (
is_admin()
&& is_search()
&& isset( $_REQUEST['post_type'] )
&& 'shop_order' == $_REQUEST['post_type']
) {
$args['post__in'] = [];
}
return $args;
}, 10, 2 );

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

Integrate with Orders Filters

By default SearchWP does not integrate with any Admin filters that have been set up. We can build in support for WooCommerce’s Order Date and Customer filters with this hook:

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

<?php
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/
// Add support for WooCommerce Admin filters when searching Orders with SearchWP.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
if ( isset( $_GET['_customer_user'] ) && ! empty( $_GET['_customer_user'] ) ) {
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) );
$mod->set_local_table( $wpdb->postmeta );
$mod->on( 'post_id', [ 'column' => 'id' ] );
$mod->on( 'meta_key', [ 'value' => '_customer_user' ] );
$mod->raw_where_sql( function( $runtime_mod ) use ( $wpdb ) {
return $wpdb->prepare( "{$runtime_mod->get_local_table_alias()}.meta_value = %d", absint( $_GET['_customer_user'] ) );
} );
$mods[] = $mod;
}
if ( isset( $_GET['m'] ) && ! empty( $_GET['m'] ) ) {
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'shop_order' ) );
$mod->raw_where_sql( function( $runtime_mod ) use ( $wpdb ) {
return $wpdb->prepare( "DATE_FORMAT( {$runtime_mod->get_local_table_alias()}.post_date, \"%Y%m\" ) = %d", absint( $_GET['m'] ) );
} );
$mods[] = $mod;
}
return $mods;
}, 99, 2 );

With that hook in place, SearchWP will apply the limits as defined by the Order Date and Customer filters when searching WooCommerce Orders.

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 );

Note: 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' ] );
}
} );
// Add Order Notes to SearchWP's Comments Source.
add_filter( 'searchwp\source\comment\db_where', function( $db_where, $source ) {
$db_where[0]['value'] = [ 'comment', 'order_note', ];
$db_where[0]['compare'] = 'IN';
return $db_where;
}, 10, 2 );

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.