WooCommerce

Relevanssi is a fine tool to enhance your WooCommerce store. In order to get the most out of Relevanssi and WooCommerce, you need to make some adjustments: everything doesn’t work perfectly straight out of the box.

These all work both with the free version and Premium.

Do not expand shortcodes

Make sure “Expand shortcodes in indexing” is disabled, because WooCommerce contains shortcodes that do not play nice with Relevanssi, causing the indexing to fail.

Index SKUs

You want to make SKUs searchable. They’re not, by default, but that’s easy to fix. Find the “Custom fields to index” setting on the Indexing tab on Relevanssi settings page and either choose to index all custom fields, or choose “Some” and add _sku to the field: that’s the name of the custom field that stores the SKU. Rebuild the index afterwards.

Relevanssi custom fields setting

Fix hyphens

SKUs often contain hyphens like this: “123456-123”. Relevanssi sees this as “123456 123” and will find the post when searching for “123456”, “123” or “123456-123”, but not when someone searches for “123456123”. If that is not what you want, you have to change the way Relevanssi handles the hyphens.

Relevanssi hyphens and dashes
This advanced indexing settings panel can be used to adjust how the hyphens are handled.

To fix the issue, go to Indexing tab, open Advanced indexing settings and set the “Hyphens and dashes” to “Remove”. Now after you rebuild the index, hyphens will be removed and searching for “123456123” will find the post.

If you’re not seeing the Indexing tab, your Relevanssi version is too old. Update to the latest version.

For more details about punctuation control, see this knowledge base entry.

Search for other post types

WooCommerce search is usually restricted to just products. If you want to include other post types in the  search results, make sure the post type restriction is lifted. Sometimes this can be adjusted from the theme settings, sometimes you have to edit the search form HTML code, in which case the restriction usually looks like this:

However, many WooCommerce themes have a product-style search page layout that only triggers when the post_type is set to “product”.

Here’s how you can force the theme to show both posts and products in the search, just add this to the theme functions.php:

add_filter( 'relevanssi_modify_wp_query', 'rlv_force_post_product' );
function rlv_force_post_product( $query ) {
    $query->query_vars['post_types'] = 'post,product';
    return $query;
}

Product variations

Relevanssi can index products and product variations, depending on how you set up the indexing. If the post type is not restricted to just “product”, the search will return also product variations.

If you want to index product variation SKUs as part of the parent product, so that you can find the parent product when searching for variation SKU, you can find the instructions in this Knowledge Base entry.

Keep excluded products out of the search

If you don’t want the search to show excluded products, you can find the instructions in this Knowledge Base entry.

Use popularity and price sorting

Relevanssi by default doesn’t understand price and popularity sorting. You have to teach Relevanssi how to do that. The procedure is explained in this Knowledge Base entry.

Product category parameters do not work?

If you try to add product categories to searches like this https://www.example.com/?s=term&product_cat=category, you’ll find the restriction may not work with WooCommerce 3. That’s because WooCommerce uses taxonomy filters to block non-visible products from the search, and that makes Relevanssi pass the product_cat parameter.

To make sure it’s involved, add this function to your theme functions.php:

add_filter( 'relevanssi_modify_wp_query', 'rlv_include_product_cat' );
function rlv_include_product_cat( $query ) {
	if ( isset( $query->query_vars['product_cat'] ) ) {
		$query->query_vars['tax_query'][] = array(
			'taxonomy' => 'product_cat',
			'field' => 'slug',
			'terms' => $query->query_vars['product_cat'],
			'include_children' => false,
		);
	}
	return $query;
}

This will include the product_cat parameter to the search. The code is set to exclude children categories, but if you want the category filter to include children categories when you restrict the search to parent categories (say, if you have a clothing store and have parent categories “Women”, “Men” and “Children”, you probably want “Women’s Shirts” subcategory included when restricting to parent category “Women”), change the false on the include_children line to true.

Use Relevanssi in admin product search

WooCommerce blocks Relevanssi in the admin product search at least from version 3.3.4 onwards. In order to have Relevanssi results in the WooCommerce admin product search, add this code to your theme functions.php:

add_filter( 'relevanssi_search_ok', 'rlv_product_search_override', 10, 2 );
function rlv_product_search_override( $ok, $query ) {
	if ( isset( $query->query_vars['product_search'] ) ) $ok = true;
	return $ok;
}
 
add_filter( 'relevanssi_modify_wp_query', 'rlv_product_search_modify' );
function rlv_product_search_modify( $query ) {
	if ( isset( $query->query_vars['product_search'] ) ) {
		$s = $_GET['s'];
		$query->set( 's', $s );
	}
	return $query;
}

Disable single search result redirect

WooCommerce has a feature that automatically redirects searches to the product page if there’s just one search result found. If you find this annoying, you can disable the feature by adding this line to your theme functions.php:

add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );

WooCommerce and Polylang

WooCommerce and Polylang don’t always play nice together. Both use tax queries – WooCommerce to hide some products, Polylang to filter the languages – and these two can clash bad. In this clash WooCommerce overrides Polylang, and the language filtering won’t work the way you want it to work.

If you’re running into problems where your language filtering is not working properly, add this to your theme functions.php:

add_filter( 'relevanssi_modify_wp_query', 'rlv_reinforce_polylang' );
function rlv_reinforce_polylang( $query ) {
	$tax_query = $query->query_vars['tax_query'];
	$tax_query[] = array(
		'taxonomy' => 'language',
		'field' => 'slug',
		'terms' => array( $query->query_vars['lang'] ),
	);
	$query->query_vars['tax_query'] = $tax_query;
	return $query;
}

This will take the language parameter from the lang parameter (where it works, if WooCommerce is not involved) and adds it to the tax_query.

Weight boost for in-stock products

To give boost for products that are available, you can add this function to your theme functions.php. This code is untested and not 100% guaranteed to work, but if not perfect, it’s at least pretty close to what works.

add_filter( 'relevanssi_match', 'rlv_woo_stocklevel' );
function rlv_woo_stocklevel( $match ) {
    $in_stock = true;
    if ( has_term( 'outofstock', 'product_visibility', $match->doc ) ) {
      // Product is not in stock.
      $in_stock = false;
    }
    if ( $in_stock ) {
      // If product is in stock, multiply the weight by 10.
      $match->weight *= 10;
    }
    return $match;
}