If you have Advanced Woo Search plugin installed on your Woocommerce site and you are facing the need to not show some specific product statuses in ajax search, then my snippet will help you. For example, I have written a code that does not show items with discontinued status in ajax search, but shows items with in stock and out of stock status.
Initially, I ran into the problem that my code was not working for ajax searches (Nothing found), but was showing the correct results in a normal search. Try changing the following settings to get it to work correctly:
Add this code to your child theme’s functions.php file.
//Hide item with Discontinued status from AWS search
add_filter( 'aws_search_results_products_ids', 'aws_search_results_products_ids', 10, 2 );
function aws_search_results_products_ids( $products, $s ) {
$filtered_products = array();
foreach ( $products as $product_id ) {
$product_status = wc_get_product( $product_id )->get_stock_status();
if ( 'discontinued' !== $product_status ) {
$filtered_products[] = $product_id;
}
}
return $filtered_products;
}
Leave a Reply