Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Show products from current product category (when viewing a single product)
<?php
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
if ($r->have_posts()) {
?>
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>
</ul>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_query();
}
}
?>
@georgybu

This comment has been minimized.

Copy link

commented Dec 14, 2012

Woocommerce get next / previous product

@timothydecker

This comment has been minimized.

Copy link

commented Apr 18, 2013

Why is it that when I try to query posts with a parent product_cat, eg 'Our Products', I am returned posts that match a child product_cat of that parent, eg 'Our Products -> Viewfinders', even when the only product_cat selected for a product, eg 'Pocket Mini Viewfinder', is 'Viewfinders'?

So, to explain again, I have two product_cat in a hierarchical relationship, 'Our Products -> Viewfinders'. I have products in both the 'Our Products' product_cat and in the 'Viewfinders' product_cat. I want to query posts with JUST the 'Our Products' product_cat. But when I use a query like the one you posted above, I get included posts whose only product_cat is 'Viewfinders'. Is there a way around this?

@scottdurban

This comment has been minimized.

Copy link

commented Mar 11, 2015

Thank you thank you thank you dude!
I used your code for a related product issue I was having. Related products weren't sticking to their categories. They were deeming themselves related to every single product, making the feature stupid. By adding in the 'orderby' => 'rand' argument, and inserting your other args and conditionals, it no is behaving the way I imagine it is supposed to! Saved me hours my man!

For any one who is interested, this is what my final related.php file looked like.

<?php
/**
 * Related Products
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( 'orderby' => 'rand', 'post__not_in' => array( $post->ID ), 'posts_per_page' => 4, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
if ($r->have_posts()) { ?>



    <div class="related products">
        <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>

        <?php woocommerce_product_loop_start(); ?>

            <?php while ($r->have_posts()) : $r->the_post(); global $product; ?>

                <?php wc_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    </div>

<?php

wp_reset_query();
}
}
@Windygo

This comment has been minimized.

Copy link

commented Sep 25, 2015

Hi,

I get a list of products in the current parent category. Any idea how to tweak it so that the products listed belong only to the deepest subcategory of the current category?

thanks

@prashantpandey10

This comment has been minimized.

Copy link

commented Oct 16, 2015

I am not able to access the current product id from a custom plugin I have made for some purpose. Can anyone help me in this.

@jasperf

This comment has been minimized.

Copy link

commented Dec 14, 2016

@timothydecker As long as you do not tick the parent category and only tick the sub category it should only load in the sub category. I realized the dummy data had albums and singles under their own categories AND in the parent category music. So that is how they got loaded all of them under different sub categories. But this is not the solution either. I still want all music items under music. I just do not want to show albums under single

@aspiron

This comment has been minimized.

Copy link

commented Feb 13, 2018

brothers who can give me sql form of this codes

@Usha-Kalamani

This comment has been minimized.

Copy link

commented Feb 27, 2018

@mikejolley 'hierarchical' => 1, is not working with the code given by you. Can you please guide me? Because, i want to display only one child level products.

Regards,
Usha

@asifmarp

This comment has been minimized.

Copy link

commented Nov 4, 2018

@scottdurban....your code ...working fine...do you have any idea? ...how to display it as a slider?...

@erdcck

This comment has been minimized.

Copy link

commented Nov 13, 2018

Hi
working fine thank you. How can I do these products as html list box.
I want to add this box like as WooCommerce product variation view.

thank you.

@NCleveland540

This comment has been minimized.

Copy link

commented Jun 11, 2019

Is this code meant to work with all product categories on a site or do I need to direct it at a specific child or parent?

@gravitymaster97

This comment has been minimized.

Copy link

commented Aug 22, 2019

Woocommerce Product list Widget is also one good in widget show category wise products

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.