Show subcategories of a particular product!

Asked

Viewed 13 times

-2

Good afternoon! I have been with this problem for some time, I am trying to list the sub category of certain product of Woocommerce. As far as I can, you’re listing all the subcategories in my category. I know I have to identify the post id of the subcategory, but I tried several ways and could not solve.

function tutsplus_product_subcategories( ) {
    $categories = get_terms($post,
    array(
        'taxonomy'   => 'product_cat',
        'parent'     => 0, // <-- No Parent
        'orderby'    => 'term_id',
        'hide_empty' => true // <!-- change to false to also display empty ones
    )
);

foreach ($categories as $category) {
    $cat_id   = $category->term_id;

    // Get all the subcategories that belong to the current category
    $subcategories = get_terms(
        array(
            'taxonomy'   => 'product_cat',
            'parent'     => $cat_id, // <-- The parent is the current category
            'orderby'    => 'term_id',
            'hide_empty' => true
        )
    );

    if ( $subcategories ) {
        echo '<ul class="product-cats">';

        foreach ( $subcategories as $subcategorie ) {

            $subcat_name = $subcategorie->name;
            echo '<a class="estilo-texto-categoria"href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
            echo    $subcat_name;
            echo '</a>';
        }

        echo '</ul>';
    }
}

}A imagem de como está ficando os produtos

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.