Exclude products in some categories from the shop

This code removes whole categories from appearing anywhere in the shop. Add the category name to the array in the code.

 * Exclude products in categories from the shop

add_action('woocommerce_product_query', 'sc_woo_exclude_category_from_shop');  

function sc_woo_exclude_category_from_shop($q) {

  $tax_query = (array) $q->get('tax_query');

  $tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => array('widgets', 'dongles'), // Don't display products in the widgets and dongles categories
    'operator' => 'NOT IN'

  $q->set('tax_query', $tax_query);