Support » Plugin: Ajax Search Lite » Woocommerce – Hide out of stock items from search results

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author wpdreams

    (@wpdreams)

    Hi!

    There is no option for that unfortunately, as stock status is stored in a custom field and not as a post status.

    Luckily there is a way to filter them anyways. I’ve quickly put together a filtering function, which parses through the results, and checks the post status. If not in stock, the product is removed from the results list. Put this code to your themes functions.php file:

    add_filter( 'asl_pagepost_results', 'asl_stock_status_filter', 1, 1 );
    
    function asl_stock_status_filter( $pageposts ) {
      $new_results = array();
    
      foreach ($pageposts as $k=>$v) {
    
        // Get the stock status
        $stock_status = get_post_meta( $v->id, '_stock_status', true);
    
        // No stock status? Probably a post or page, so append it and continue.
        if ( $stock_status == "" ) {
            $new_results[] = $v;
            continue;
        }
    
        // In stock? Append of course.
        if ( $stock_status == "instock" )
           $new_results[] = $v;
    
      }
    
      return $new_results;
    }

    Let me know if it works. This should be update proof as well.

    Your code doesn’t work on my site.

    Is it possible to change some codes in this file? /public_html/wp-content/themes/zorka/lib/ajax-action/search-product-ajax-action.php

    if ( $keyword ) {
    
                    $search_query = array(
    
                            'search_prod_title' => $keyword,
    
                            'order'             => 'DESC',
    
                            'orderby'           => 'date',
    
                            'post_status'        => 'publish',                        
    
                            'post_type'         => array('product'),                        
    
                            'nopaging' => true,
    
                    );

    pundurbrother

    (@pundurbrother)

    I am in for this too! Who would want their out of stock items appear in search results, except those who are available for backorder?

    I think every shop have hundreds of old, sold and out of stock items. Why would mislead customers to be able to search for items they can not buy anymore???

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce – Hide out of stock items from search results’ is closed to new replies.