Skip to main contentSkip to footer

WooCommerce: Make Relevanssi notice stock changes

In some cases, WooCommerce product stock level changes don’t affect the search immediately. If the search is set up to exclude out-of-stock items, a product that sells out may still be included in the search until the next reindexing.

This happens because changes in the stock status don’t trigger a WordPress post update that Relevanssi listens to, and Relevanssi doesn’t know the post has changed. Thus the change in stock status goes unnoticed until something saves the post or rebuilds the index.

The fix for this is simple, fortunately. You can make Relevanssi listen to product stock status changes. Add these functions to your site:

add_action( 'woocommerce_product_set_stock_status', function( $product_id ) {
    relevanssi_insert_edit( $product_id );
} );

add_action( 'woocommerce_variation_set_stock_status', function( $product_id ) {
    relevanssi_insert_edit( $product_id );
} );

Now whenever a product stock status changes, Relevanssi is notified and reindexes the post. This will remove the post from the index if it goes out-of-stock and out-of-stock items are excluded.

This was originally asked on WP.org forums.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

CM Tooltip Glossary
A Relevanssi Premium customer bumped into a weird problem with CM Tooltip Glossary (and probably Elementor). Using Elementor, CM Tooltip…
How to include specific posts
Hello, I’m using WooCommerce and Relevanssi and they work great.My Site Search only searches Products, however I want to let…

Related Posts:

Comment Section:

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed