Indexing and Searching WooCommerce Product Variation SKUs

Last updated December 18, 2018 « Knowledge Base

Using a couple of SearchWP’s hooks you can easily incorporate your Variable Product SKUs into SearchWP searches.

WooCommerce stores Product Variations as separate posts, so the first step will be to tell SearchWP to store some custom metadata for the parent product (the one that contains all of the Variations with SKUs). We can do that using the searchwp_extra_metadata hook by adding something like the following to your theme’s functions.php:

<?php
// index WooCommerce product_variation SKUs with the parent post
function my_searchwp_index_woocommerce_variation_skus( $extra_meta, $post_being_indexed ) {
// we only care about WooCommerce Products
if ( 'product' !== get_post_type( $post_being_indexed ) ) {
return $extra_meta;
}
// retrieve all the product variations
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $post_being_indexed->ID,
);
$product_variations = get_posts( $args );
if ( ! empty( $product_variations ) ) {
// store all SKUs as a Custom Field with a key of 'my_product_variation_skus'
$extra_meta['my_product_variation_skus'] = array();
// loop through all product variations, grab and store the SKU
foreach ( $product_variations as $product_variation ) {
$extra_meta['my_product_variation_skus'][] = get_post_meta( absint( $product_variation ), '_sku', true );
}
}
return $extra_meta;
}
add_filter( 'searchwp_extra_metadata', 'my_searchwp_index_woocommerce_variation_skus', 10, 2 );
view raw gistfile1.php hosted with ❤ by GitHub

Note: we are storing the Variation SKUs as a Custom Field named my_product_variation_skus. Since we are using ‘extra’ metadata, we will also need to add this Custom Field name to the SearchWP settings page by adding the following to our theme’s functions.php:

<?php
// reference: https://gist.github.com/jchristopher/0cad8418fd9477c57b53
function my_searchwp_custom_field_keys_variation_skus( $keys ) {
$keys[] = 'my_product_variation_skus';
return $keys;
}
add_filter( 'searchwp_custom_field_keys', 'my_searchwp_custom_field_keys_variation_skus', 10, 1 );
view raw gistfile1.php hosted with ❤ by GitHub

This second snippet will add a new Custom Field key to the SearchWP settings named my_product_variation_skus — this is what the first snippet is using to store all of the Variation SKUs. You will need to add this Custom Field to your SearchWP engine settings in order for the Variation SKUs to be considered during searches:

Add your Custom Field to the engine

 

The last (important) step is to rebuild your index. This needs to be done because all of your Products have already been indexed without the first snippet also indexing the Product Variation SKUs. Once the index has been rebuilt and your settings saved with the my_product_variation_skus Custom Field, WooCommerce Variable Product SKUs will be included in search results.

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