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

Add WooCommerce Product Variations to Products

WooCommerce treats Product Variations differently than Products; they’re separate post types. Products are public and can be utilized in the way we’re used to working with post types, but Product Variations are different in that they’re set up as children of Products, but a different post type.

It’s a neat way to set things up, but we need to use a small bit of custom code to make Product Variations searchable.

The setup will involve teaching SearchWP about Product Variations, and having SearchWP index Product Variations as it is indexing the parent Products themselves. We’ll store the Product Variation data as ‘extra’ metadata for the Products, so when you search for Product Variation data the proper Product will be returned.

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

// Add WooCommerce Product Variation data to parent Products in SearchWP.
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
// If this is not a Product, there's nothing to do.
if ( 'product' !== get_post_type( $entry->get_id() ) ) {
return $data;
$my_extra_meta_key = 'searchwp_product_variations';
// Retrieve all Variations.
$product_variations = get_posts( [
'post_type' => 'product_variation',
'posts_per_page' => -1,
'fields' => 'ids',
'post_parent' => $entry->get_id(),
] );
if ( empty( $product_variations ) ) {
return $data;
$data['meta'][ $my_extra_meta_key ] = $product_variations;
return $data;
}, 20, 2 );
// Add our Extra Meta entry to SearchWP's UI.
add_filter( 'searchwp\source\attribute\options\special', function( $keys, $args ) {
if ( $args['attribute'] !== 'meta' ) {
return $keys;
// This key is the same as the one used in the searchwp\entry\data hook above, they must be the same.
$my_extra_meta_key = 'searchwp_product_variations';
$option = new \SearchWP\Option( $my_extra_meta_key, 'Product Variations' );
// If there's already a match, remove it because we want ours there.
$keys = array_filter( $keys, function( $option ) use ( $my_extra_meta_key ) {
return $my_extra_meta_key !== $option->get_value();
} );
// Add "Product Variations" Option
$keys[] = $option;
return $keys;
}, 20, 2 );

With that snippet in place we can add Product Variations to the Custom Fields of our Engine:

Once you save your Engines, SearchWP will let you know the index needs to be rebuilt because of this change. The index can be rebuilt using the Rebuild Index button on the Engines tab of the SearchWP settings screen.

As the index rebuilds, SearchWP will pull in all Product Variations as it indexes Products, making all of that Product Variation data searchable and returning the parent Product when a Product Variation match is found.

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