I would like to change the related products section on my woocommerce products details page so it only displays related child category products (not products that only have the parent category in common). For Example:
- Parent Cat
- Child 1 Cat
- Child 2 Cat
- Child 3 Cat
If a user is visiting a product details page that is categorized under "Parent Category -> Child 1 Cat" all the user is viewing are "Child 1 Cat" products.
I am attempting to modify the woocommerce/single-product/related.php
file inside of my theme folder using the following php scripts to accomplish this behavior:
$product_cat_slugs = array();
foreach (get_the_terms($product->ID, 'product_cat') as $term) {
if($term->slug !== 'skip-this-cat') {
array_push($product_cat_slugs, $term->slug);
}
}
$args = array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'orderby' => 'rand',
'post__not_in' => array( $product->id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_cat_slugs
)
)
);
$products = new WP_Query( $args );
The categorization is correct, but my problem is that I am not getting back a random set of posts. I'm only receiving the same posts each load.
skip-this-cat
) of the current product$query->posts
directly? Or... you're saying it would be the same 4 each time... nm.