Posted on

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.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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