Posted on

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.

16 comments WooCommerce: Indexing product variation SKUs for main product

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

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

  2. 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.

    1. 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.

  3. 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?

    1. 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.

      1. 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!

  4. 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?

    1. 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.

    1. Dmitriy, open the “Advanced indexing settings” on the “Indexing” tab of Relevanssi settings, set “Hyphens and dashes” to “Remove” and rebuild the index.

  5. 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 😀

  6. 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

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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