How to exclude products from certain categories?

The solution works only for the Pro plugin version.

Currently, there is no possibility to exclude a product from search via the plugin settings. The solution requires custom PHP snippets. To do it, you have to paste the following code into functions.php in your child theme or use Code Snippets plugin.

To implement the code correctly, you have to know IDs of categories, you want to exclude. You can read category ID from the URL when you are on the category edit page. Take a look at the screen:


After implementing the code, Remember to rebuild the search index. To do it got to WooCommerce -> AJAX Search bar -> Indexer (tab) and click “Rebuild index” button.

Custom code

/**
 * IDs of products categories to exlude from search
 *
 * @return array
 */
function get_product_categories_to_exclude(){
    return array(
        3060, //Replace with yours!!!
        1223  //Replace with yours!!!
    );
}


/**
 * Prevent indexing of products that belong to the specific category after bulk indexing
 *
 * @param string $where
 */
add_filter('dgwt/wcas/tnt/source_query/where', function ($where) {
    global $wpdb;
    $to_exclude = get_product_categories_to_exclude();
    
    $placeholders = array_fill(0, count($to_exclude), '%d');
    $format = implode(', ', $placeholders);
    $where .= $wpdb->prepare(" AND posts.ID NOT IN (
                    SELECT object_id
                    FROM $wpdb->term_relationships
                    WHERE term_taxonomy_id IN ($format)
        )",
        $to_exclude
    );
    return $where;
});


/**
 * Prevent indexing of products that belong to the specific category after edit product
 *
 * @param string $html output of method WC_Product::get_price_html()
 * @param int $product_id
 * @param WC_Product $product
 */
add_filter('dgwt/wcas/indexer/updater/can_index', function ($can_index, $product_id, $product) {
    if ($can_index) {
        $cat_ids = $product->get_category_ids();
        $to_exclude = get_product_categories_to_exclude();
        
        if (!empty($cat_ids) && is_array($cat_ids)) {
            foreach ($cat_ids as $cat_id) {
                if (in_array($cat_id, $to_exclude)) {
                    $can_index = false;
                    break;
                }
            }
        }
    }
    return $can_index;
}, 10, 3);