Skip to main contentSkip to footer

WooCommerce: Indexing product variation SKUs for main product

Relevanssi can index product and product variation SKUs for WooCommerce products: just add _sku to list of custom fields to index.

However, if you want to find the main product when searching for the product variation SKU, you need some extra code using the relevanssi_content_to_index filter hook.

Add this function to your site and rebuild the index:

add_filter( 'relevanssi_content_to_index', 'rlv_index_variation_skus', 10, 2 );
function rlv_index_variation_skus( $content, $post ) {
	if ( 'product' === $post->post_type ) {
		$args       = array(
            'post_parent'    => $post->ID,
			'post_type'      => 'product_variation',
			'posts_per_page' => -1
        );
		$variations = get_posts( $args );
		if ( ! empty( $variations ) ) {
			foreach ( $variations as $variation ) {
				$sku      = get_post_meta( $variation->ID, '_sku', true );
				$content .= " $sku ";
			}
		}
	}
	return $content;
}

This will index the product variation SKUs for the main product, and you will be able to find the main product when searching for the variation SKU.

See more WooCommerce tips.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

WP Event Manager
Using Relevanssi with WP Event Manager requires you to adjust the search process in WP Event Manager a bit. Fortunately,…
Why is a post type excluded from search?

…is registered has not changed: it’s still the same. Relevanssi just doesn’t care about it and still indexes the post. The setting does affect Relevanssi in that when Relevanssi does search posts, Relevanssi does respect this setting and will exclude these posts from the searches. That’s often for the best;…In the list of post types to index in the Relevanssi indexing settings tab there’s a column “Excluded from search”, which has a value of “yes” or “no” for each post type. What does this mean, and why does the value remain “yes” even if you set Relevanssi to index……that post type? This column is strictly for information only, and does not govern what Relevanssi does. This is something that is defined when a post type is registered. Some post types are meant for public use (like post or page) and thus are not excluded from the search. Other…

Martfury
Martfury is a WooCommerce theme that comes with a built-in dropdown search. That’s great, except that as usual the dropdown…

Related Posts:

Comment Section:

16 Comments. Leave new

  • How can I please use this in conjunction with
    add_filter( ‘relevanssi_index_content’, ‘__return_false’ );

    I want to index variation SKUs but not index page content. The above had been taken from one of your other articles to exclude content but that also seems to prevent the above code for variations running. I can test this by using debug code in this function which does not run when the mentioned filter is included.

    Many Thanks

    Reply
  • Thanks Mikko
    I have tryed just about every plugin and searching for codes, that would help me to make a seach on SKU.
    Relevanssi solved it all and even gave me more options, like the redirect function.
    Thanks 😀

    Reply
  • How can I customize this? SKU number for a dash and without —> 12345 and 123-45?

    Reply
  • is there any provission to show variable product image on search results?

    example Suppose I have a bag with red and blue variants with red featured image, if someone searches for blue bag, weather its possible to display blue bag as featured insted of red?

    Reply
    • Sujo, sure it’s possible – but it’s not Relevanssi’s job. Your theme is responsible for showing the search results, and any featured images come from your theme. Now you can definitely set up your theme in a way that there are conditionals for the featured images and the correct variation is shown, but it’s going to be a bit of manual labor involved in it.

      Another option is to make Relevanssi index product variations and have those show up in the results, in which case it would be specifically the blue bag that appears in the results. Whether that makes sense depends on how your site and your products are set up; in general WooCommerce does prefer to show products and not variations.

      Reply
  • Emilio Navarro
    July 13, 2018 11:25 am

    Hi,

    I´ve been using this function and it works fine, but the thing is that I want the search tool to show me the exact product variation that I´m looking for, not the main product. I´ve tried indexing the product variations but it shows me nothing.

    What can I do to see first the product variation instead of the main product?

    Reply
    • Emilio, indexing product variations is the correct way to go. However, WooCommerce themes generally restrict the search to just products, which will leave the product variations off. See Relevanssi WooCommerce manual and “Search for other post types” for a way to get product_variation included.

      Reply
      • Emilio Navarro
        July 13, 2018 11:44 am

        Thanks! You nailed it.

        I´ve added product_variation to the function in ” Search for other post types” then pasted this to funtions.php and it works exactly the way I wanted.

        Cheers!

        Reply
  • Hello.

    This code works but you have to rebuild the entire index everytime so that the variation sku find the product. If you click index unindexed posts nothing happens because the are already index but not with the variation sku.

    Is there a work around?

    Thank you.

    Reply
    • No need to reindex the whole index, just save the original product post so that it gets reindexed. You could probably create an action on wp_insert_post or something that would trigger reindexing for the parent post when a variation is saved or something like that.

      Reply
  • This code has been compromised, if pasted directly it breaks WordPress entirely.

    To use this code, you must replace all occurrences of > with >

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed