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

Italian plurals
A Relevanssi user asked about Italian plurals on the Relevanssi support forums. The best answer is to use the Relevanssi…
CodeMirror Blocks
CodeMirror Blocks is a good plugin for displaying good-looking code blocks in WordPress (you can see it in action right…
Results are in strange order
If best results don’t come up first and changing weighting settings doesn’t seem to make any difference, there couple of…

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