Hide a WooCommerce Category from Search Results

Code to Hide a WooCommerce Category from Search Results-

function hide_rentals_from_search_pre_get_posts( $query ) {
 
 if (!is_admin() && $query->is_main_query() && $query->is_search()) {
 
 $query->set( ‘post_type’, array( ‘product’ ) );
//in the current case i want to hide “rental” category, you can easily replace this with some other product category slug
 $tax_query = array(
 array(
 ‘taxonomy’ => ‘product_cat’,
 ‘field’ => ‘slug’,
 ‘terms’ => ‘rentals’, 
 ‘operator’ => ‘NOT IN’,
 ),
 );
$query->set( ‘tax_query’, $tax_query );
 }
 
}
add_action( ‘pre_get_posts’, ‘hide_rentals_from_search_pre_get_posts’);