WooCommerce

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

These all work both with the free version and the Premium.

Separate articles

Table of contents

Search finds no products (WooCommerce 4.4+)

With WooCommerce version 4.4 and newer, there can sometimes be problems with the product search not working. Relevanssi fixes most of these problems automatically, but sometimes the solution provided by Relevanssi does not work.

If searching for products doesn’t find anything, but the product search works in the Relevanssi admin search (Dashboard > Admin search), the problem is caused by having a product archive template that does not run the woocommerce_before_shop_loop action.

You need to modify the product archive template in your theme (or child theme) to fix these problems. Either make sure it runs the woocommerce_before_shop_loop action before the product loop or that it calls wc_reset_loop() directly.

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.

Indexing SKUs

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

Relevanssi custom fields setting

Hyphens in SKUs

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 how Relevanssi handles the hyphens.

Relevanssi hyphens and dashes
You can use this advanced indexing settings panel to adjust how Relevanssi handles the hyphens.

To fix the issue, go to the Indexing tab, open Advanced indexing settings and set the “Hyphens and dashes” to “Remove”. 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 themes usually restrict the WooCommerce search to just products. To include other post types in the search results, remove the post type restriction. Sometimes you can adjust this from the theme settings, and sometimes you have to edit the search form HTML code, in which case the restriction usually looks like this:

<input name="post_type" type="hidden" value="product">

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; add this to your site:

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. The search will also return product variations if the theme doesn’t restrict the post type to just “product”.

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

Problems with product category parameters

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, making Relevanssi pass the product_cat parameter.

To make sure it’s involved, add this function to your site:

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 snippet will include the product_cat parameter to the search. The code excludes 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.

WooCommerce blocks Relevanssi in the admin product search from version 3.3.4 onwards. To have Relevanssi results in the WooCommerce admin product search, add this code to your site:

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'] )
         && $query->query_vars['post_status'] !== 'trash' ) {
         $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'] ) ) {
		unset( $query->query_vars['post__in'] );
		$query->query_vars['s'] = $_REQUEST['s'];
	}
	return $query;
}

Disabling the 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 snippet to your site:

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 badly. 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 correctly, add this to your site:

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 snippet 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 boost available products, you can add this function to your site. Note that this is likely a major performance problem, see this documentation to make this run faster and more efficiently.

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;
}

WooCommerce Price Filter widget

The WooCommerce Price Filter widget doesn’t work with Relevanssi; the price information doesn’t get passed on to Relevanssi. Add this function to your site to make Relevanssi filter products by the min_price and max_price parameters from the WooCommerce widget.

add_filter( 'relevanssi_hits_filter', 'rlv_price_filter' );
function rlv_price_filter( $hits ) {
    $min_price = isset( $_GET['min_price'] ) ? intval( $_GET['min_price'] ) : 0;
    $max_price = isset( $_GET['max_price'] ) ? intval( $_GET['max_price'] ) : 0;
    if ( ! $min_price || ! $max_price ) {
        return $hits;
    }
    $filtered_products = array();
    foreach( $hits[0] as $hit ) {
        if ( 'product' !== $hit->post_type ) {
            continue;
        }
        $product = wc_get_product( $hit->ID );
        $price   = $product->get_price();
        if ( $price >= $min_price && $price <= $max_price ) {
            $filtered_products[] = $hit;
        }
    }
    $hits[0] = $filtered_products;
    return $hits;
}

Other problems with WooCommerce filter widgets (Layered Navigation)

The WooCommerce filtering widgets generally work fine with Relevanssi, as long as you’re not searching for anything that the default WP search can’t find. The WooCommerce filters don’t use Relevanssi to get information (prices, categories, and so on). The WooCommerce filters only look at posts where the search term is in the title, content or excerpt. For example, if Relevanssi finds the post by a category match, the filters don’t see that.

Starting from Relevanssi 4.17.0 (Premium 2.19.0), Relevanssi can take over the filtering queries. Add this line to your site:

add_filter(	'woocommerce_get_filtered_term_product_counts_query', 'relevanssi_filtered_term_product_counts_query' );

This line will use Relevanssi data in the filtering queries. This feature isn’t automatically enabled because it can lead to problems. Try it out and see how it works for you. If you want a better solution, Advanced AJAX Product Filters is a fine plugin that uses Relevanssi without problems.

Shop Managers can’t access User Searches

Shop Managers should have access to the User Searches page in the admin, but in case they don’t, this snippet fixes the problem using the relevanssi_user_searches_capability filter hook:

add_filter( 'relevanssi_user_searches_capability', function() { return 'manage_woocommerce'; } ); 

Indexing products from WooCommerce API

When you add products with the WooCommerce API, Relevanssi indexes them, but only the basic product details and nothing else. First, WooCommerce creates the post, Relevanssi indexes it, then details like the SKU are added, and these are not indexed.

You can use the woocommerce_new_product action hook to make Relevanssi index the post after the WooCommerce product creation process finishes:

add_action(
  'woocommerce_new_product',
  function( $id ) {
    relevanssi_index_doc( $id, true, relevanssi_get_custom_fields(), true );
  }
);