Exclude products by id in shop and category page

Add this code in your activated theme in functions.php file. You can replace the product id with your own product ids which you want to hide from the frontend in wordpress woocommerce. If you want to hide category from frontend in woocommerce. You can see this post : Hide particular category from frontend

add_action( 'woocommerce_product_query', 'cf_exclude_custom_pre_get_posts_query' );
function cf_exclude_custom_pre_get_posts_query( $q ) {

	$args = array(
		'numberposts' => -1,
		'post_type'   => 'product',
		'fields' => 'ids',
		'post_status' => 'publish',
	  );
	   
	$products = get_posts($args);
	$ids = array('1812','127'); // product ids
        $q->set('post__not_in' , $ids);

}