0

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.

  • Are you sure there are more than 4 products for the given query? – TheDeadMedic Aug 15 '16 at 13:50
  • Yes, there are about 120 products right now total and more than four under each child category. It seems like the randomize only stops working when the tax_query lines are added. – adam2k Aug 15 '16 at 14:59
  • I think we've found your problem - if the tax_query "breaks" the randomization, then it's because there aren't more than 4 products that match all the categories (other than skip-this-cat) of the current product – TheDeadMedic Aug 15 '16 at 15:00
  • The 'skip-this-cat' is the parent category. I'm trying to get all the other categories from a specific post besides that parent category. I'll do some test output to confirm the category slug array, but the products I have entered I only have one or two categories per product and I only have a total of 5 categories, so there are plenty of products inside of each category. – adam2k Aug 15 '16 at 15:38
  • Can't you just randomize the $query->posts directly? Or... you're saying it would be the same 4 each time... nm. – jgraup Aug 16 '16 at 2:23
0

It appears that the WP_Query was correct. I am hosting with WP_Engine and I had to contact their support desk and have the ticket elevated to resolve the issue. Apparently the query, even with the 'orderby' => 'rand', was being cached by their servers. I should have contacted them earlier, but I thought it was something that I was doing incorrectly in my query. Thanks all for the help!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.