Show only last WooCommerce category products in related section


If you want to only show products from the last category inside your WooCommerce related products section, then place the code below into your functions.php.

add_filter( 'woocommerce_get_related_product_cat_terms', 'last_child_cats_only', 10, 2 );
function last_child_cats_only( $term_ids, $product ) {
	if(!$product) {
		return $term_ids;

    $cats_array = array();
    $terms = wp_get_post_terms($product, 'product_cat');
    foreach ( $terms as $key => $term ){
        $check_for_children = get_categories(array('parent' => $term->term_id, 'taxonomy' => 'product_cat'));
            $cats_array[] = $term->term_id;

    if(!empty($cats_array)) {
    	$term_ids = $cats_array;

   	return $term_ids;


Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Paramètres de confidentialité sauvegardés !
Privacy Settings

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. Control your personal Cookie Services here.

These cookies are necessary for the website to function and cannot be switched off in our systems.

In order to use this website we use the following technically required cookies
  • wordpress_test_cookie
  • wordpress_logged_in_
  • wordpress_sec

Decline all Services
Accept all Services