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.
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.
In order to fix these problems, you need to modify the product archive template in your theme (or child theme). 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.
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.
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.
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:
<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, 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'] ) ) { unset( $query->query_vars['post__in'] ); $query->query_vars['s'] = $_REQUEST['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; }
Search for orders
If you want to search for WooCommerce orders, that requires couple of extra steps beyond just checking the shop_order
post type from the list of post types to index. WooCommerce uses specific statuses for orders and by default Relevanssi does not index those. So, for starters, you need to tell Relevanssi that indexing completed WooCommerce orders is fine:
function rlv_add_status( $status_array ) { $status_array[] = 'wc-completed'; return $status_array; } add_filter( 'relevanssi_valid_status', 'rlv_add_status' ); add_filter( 'relevanssi_valid_admin_status', 'rlv_add_status' );
Second, you need to tell Relevanssi that in searching, it’s fine to show the orders:
add_filter( 'relevanssi_post_ok', 'relevanssi_show_orders', 11, 2 ); function relevanssi_show_non_published( $ok, $post_id ) { if ( 'wc-completed' === relevanssi_get_post_status( $post_id ) ) { $ok = true; } return $ok; }
WooCommerce Price Filter widget
The WooCommerce Price Filter widget doesn’t work with Relevanssi by default, the price information just doesn’t get passed on to Relevanssi. Add this function to your theme functions.php 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
The WooCommerce filtering widgets generally work fine with Relevanssi, as long as you’re not searching anything that the default WP search can’t search. The WooCommerce filters get their information (prices for the products in the price filter, the categories and so on) by doing their own searches. They don’t use the Relevanssi information. The WooCommerce filters only look at posts where the search term is found in the title, content or excerpt. If the post is found by a category match, for example, the filters don’t see that.
This means the filters may not show the correct filtering options, or may not show at all on the search page. This is unfortunately something that is really difficult to fix.
Exact match SKU searches
Here’s a useful little filter that only returns exact matches when a search term is a SKU.