1

In WooCommerce, I am trying to get all child product category terms from the parent term id of the current product category with the code below:

$category = get_queried_object();
$category_parent_id = $category->parent;
$category_test = get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );

The variable $category_parent_id returns the parent product category id and $category_test returns all product category terms

I'm trying to return all child product categories using the parent product category term id retrieved from $category->parent

Logically I tried:

$category_test = $category_parent_id->get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );

and

$category_test = $category->parent->get_terms( ['taxonomy' => 'product_cat', 'hide_empty' => false] );

But this didn't give me back all child categories of the current parent category. Anyone have any idea if and if so how this can be achieved?

1 Answer 1

2

Edited (1)

You will need to use 'child_of' from WP_Term_Query available arguments, that will let you to get all child terms for the current product category parent term:

// Only on product category archive pages
if( is_product_category() ) {
    $main_term = get_queried_object();

    $args_query = array(
        'taxonomy' => 'product_cat', 
        'hide_empty' => false, 
        'child_of' => $main_term->parent
    );

    if ( $main_term->parent != 0 ) {
        // Loop through WP_Term Objects
        foreach ( get_terms( $args_query ) as $term ) {
            if( $term->term_id != $main_term->term_id ) {
                // $term->slug; // Slug

                // Output each (linked) term name…
                echo sprintf( '<a href="%s">%s</a></br>', get_term_link( $term->term_id, 'product_cat' ), $term->name );
            }
        }
    }
}

Or you can also use get_term_children() like:

// Only on product category archive pages
if( is_product_category() ) {
    $main_term  = get_queried_object();
    $taxonomy   = 'product_cat';

    if ( $main_term->parent != 0 ) {
        $child_ids = get_term_children( $main_term->parent, $taxonomy );

        echo '<ul>';

        foreach ( $child_ids as $child_id ) {
            if( $child_id != $main_term->term_id ) {
                $term = get_term_by( 'id', $child_id, $taxonomy );
                echo '<li><a href="' . get_term_link( $child_id, $taxonomy ) . '">' . $term->name . '</a></li>';
            }
        }
        echo '</ul>';
    }
}

This is only for product category archive pages, as for product pages you can have many product categories set for a product and the code will be complicated and quiet different…

9
  • This indeed gives all desired raw data output. Any idea on just to output the name and or slug for example?
    – Jeroen_L
    Mar 19, 2020 at 20:42
  • Just accepted, if u could find the time to explain just to output name and or slug instead of raw data that would save me hours of searching. If not thanks anyhow :)
    – Jeroen_L
    Mar 19, 2020 at 20:49
  • @Jeroen_L I have updated my code to output name and or slug… Mar 19, 2020 at 21:01
  • Just noticed it won't take the parent if on a product page. And it also grabs parents of parents. and the children of those parents... any ideas? ill share my new code in next post if u are interested.
    – Jeroen_L
    Mar 19, 2020 at 22:11
  • @Jeroen_L I have edited my answer with some other code … Now on product page this is completelly different as a product can have many product categories… Mar 19, 2020 at 22:37

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.