0

I am using Wordpress with Woocommerce, I am on the detail page of the product content-product-single.php.

I created several "sub" and I have to print ONLY subcategories associated with that product.

I am using this code:

$categories = array (
        'type' => 'product',
        'child_of' => 1,
        'parent' => '8',
        'orderby' => 'term_group',
        'hide_empty' => true,
        'hierarchical' = > 1,
        'exclude' => '',
        'property' => '',
        'number' => '',
        'taxonomy' => 'product_cat',
        'pad_counts' => false);

$cats = get_categories ($categories);
foreach ($cats as $cat) {
    echo '<a <br/> href="'. get_term_link($cat-> slug,' product_cat ').' "> '. $cat-> name.'</a>';
}

Where 'parent' => '8' is the 'id of my categories FATHER

But this code print ALL subcategories that are not empty, I am interested in only those instead of THIS PRODUCT.

Thanks and good luck

1
1

Configure, product_id and parent_category_id according to your requirements.

/* This is product id */
$product_id = 1;
/* This is the id of the parent category */
$parent_category_id = 8;

$cats = get_categories ( array (
    'type' => 'product',
    'parent' => $parent_category_id,
    'orderby' => 'term_group',
    'hide_empty' => true,
    'hierarchical' = > 1,
    'exclude' => '',
    'property' => '',
    'number' => '',
    'taxonomy' => 'product_cat',
    'pad_counts' => false
));

$non_empty_sub_categories_ids = array_map(create_function('$cat', 'return $cat->term_id;'), $cats);

$product_cats = get_the_terms( $product_id, 'product_cat' );
foreach ($product_cats as $product_cat) {
    if(in_array($product_cat->term_id, $non_empty_sub_categories_ids)) {
        echo '<a <br/> href="'. get_term_link($product_cat->slug,' product_cat ').' "> '. $product_cat->name.'</a>';        
    }
}
0
<?php 

$args = array(
    'taxonomy' => 'product_cat',
    'slug' => array('category_slug'),
    'hide_empty' => true
);
$product_cat = get_terms( $args );
foreach ($product_cat as $parent_product_cat){
echo '<ul> 
        <li><a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a>
            <ul> ';
                $child_args = array(
                    'taxonomy' => 'product_cat',
                    'hide_empty' => true,
                    'parent'   => $parent_product_cat->term_id
                );
                $child_product_cats = get_terms( $child_args );
                foreach ($child_product_cats as $child_product_cat) {
                    echo '<li><a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
                }
    echo '  </ul>
        </li>
    </ul>';
}

?>

1
  • Try to include some explanation in your answer as to why/how this solves the OP's issue. – butlerblog Nov 14 '19 at 14:45

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.