I have 2 product categories "current-probes"
and "accessories"
that have some subcategories "flex-ct"
, "tlar"
and "test-lead"
.
I would like to display in the shop page, my subcategories and my products. I know that WooCommerce has a setting to display the parent categories and products but not the subcategories.
I also need to prevent the individual products of the subcategories from displaying.
Here is the code that I have tried (located in my functions.php file):
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! is_admin() && is_shop() || is_product_category( array( 'current-probes', 'accessories' )) ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'flex-ct', 'tlar', 'test-lead' ),
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
This prevents the individual products of the subcategories from showing, but I need the actual subcategory to show up in the loop.
WooCommerce has a setting that displays categories and products only, basically I would like that only it is displaying the subcategories (not the parent categories) and the products. I hope that makes sense.
I have been trying to figure this out for a week and I am going crazy. I feel like there is a simple solution that I have just overlooked.
What I am doing wrong? How can I achieve this?
Thanks