Skip to main content

Exclude Out of Stock products from search

Comments

3 comments

  • Sandra Prunici

    Hi,

    Unfortunately, at the moment it isn't possible to add a condition to the Archive or Products element to exclude the "Out of stock" products but I will inform our team about this suggestion and they will investigate it deeply.

    Thank you!

    Best regards,
    Sandra

    0
  • Darren Nicholls

    That's great thanks.

    We found a solution with some php that hides all products out of stock from search.

    As follows:

    /*Hide Out of Stock from search*/
    add_action( 'pre_get_posts', 'hide_out_of_stock_in_search' );
    function hide_out_of_stock_in_search( $query ){
    if( $query->is_search() && $query->is_main_query() ) {
    $query->set( 'meta_key', '_stock_status' );
    $query->set( 'meta_value', 'instock' );
    }
    }
    0
  • Sandra Prunici

    Ohh, great! Thank you very much for sharing with us the workaround and solution you found! Hope it will be useful for other users too!

    Best regards,
    Sandra

    0

Please sign in to leave a comment.