Woocommerce get product that is in multiple categories

I am working on setting random thumbnails for each of the categories on my woocommerce site. By default a placeholder is assigned, but you can override this by uploading / assigning an image in the dashboard.

What I am wanting to do is for example, “Mens” is my parent category and “Jeans” is my subcategory. So I want to dispaly a random image of an item that is in the “Mens >> Jeans” category….. I hope this makes sense.

Read More

What I have come up with at the minute is this (in my wc_template_functions.php file)

args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => $category->name, 'orderby' => 'rand' );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) { $loop->the_post();}{ 

$image = print_r(get_the_post_thumbnail($loop->post->ID, 'shop_catalog'));}
    $image = $image[0];     

$image =  apply_filters( 'woocommerce_placeholder_img_src',$image[0] );

Which is generating the images, but the problem i’m having is that, say I have “Jeans” under the parent category “Mens” and “Womens” the picture displayed is totally ignoring whether it’s in the “Mens” or “Womens” category. So i’m getting images of womens jeans in the “Men >> Jeans”.

What i need to do is to be able to specify two categories, “Jeans” and “Mens” but i’m unsure how to do this. I’ve been told that because products are not posts I cannot specify product category names, i have to use taxonomies?

Could someone help please and point me in the right direction?

Cheers
Chris

Related posts

Leave a Reply

3 comments

  1. This is how you do it:

    $query_args = array( 
        'post_status' => 'publish', 
        'post_type' => 'product',
        'tax_query' => array(
            array(
                    'taxonomy' => 'product_cat',
                    'field' => 'id',
                    'terms' => array( 27, 25 ),
                    'operator' => 'AND'
            )
        )
    );
    
    $loop = new WP_Query($query_args);
    
    while ($loop->have_posts() )
    {
        //Setup post data
        $loop->the_post();
    
        echo get_the_title();
    
    }
    

    This will get products that are on both categories with IDs 25 and 27.
    You can also use ‘field’ => ‘slug’´+ if you rather specify by slugs, but I think using ID’s will make a faster query in this case.

  2. <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
    
       <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
    
                            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
        <h3><?php the_title(); ?></h3>
    
    <?php endwhile; ?>
        <?php wp_reset_query(); ?>
    
  3. <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
    
    <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
    
    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    
    <h3><?php the_title(); ?></h3>