Posted on

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. Just add this function to the theme functions.php and rebuild the index:

add_filter('relevanssi_content_to_index', 'rlv_index_variation_skus', 10, 2);
function rlv_index_variation_skus($content, $post) {
	if ($post->post_type == "product") {
		$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.

10 comments 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. 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.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.