• Hi,
    I want to show category hierarchy as tree view on product page, but only the selected categories for product edit screen in admin (category tags).
    Normally this will show as a comma separated, I want to list them same as selected in admin for example –
    In admin I have selected category in product edit screen –
    Apple
    – IMac
    – B200 M2
    Cicco
    – Core i3
    – Core i3
    This will show up on product page as-
    “cateogories: Apple, IMac, B200 M2, Cicco, Core i3, Core i3”
    What I want is show same layout as I selected in admin like –
    Apple
    – IMac
    – B200 M2
    Cicco
    – Core i3
    – Core i3
    is there any way I can show this as mentioned above

    https://www.ads-software.com/plugins/woocommerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Hi prashantajani,

    Showing them in hierarchical order may be a little more complicated than it seems. The core category widget does this here: https://github.com/woothemes/woocommerce/blob/master/includes/widgets/class-wc-widget-product-categories.php

    So you will need to add some logic here to accomplish this: https://github.com/woothemes/woocommerce/blob/bd4c90dbec550aefb6dca2ce7c0903e53f2110ea/templates/single-product/meta.php#L39

    Thread Starter prashantajani

    (@prashantajani)

    HI Caleb Burks,

    Thanks For reply, I really appreciate it I have added below code in meta.php –

    $categories = get_the_terms( $post->ID, ‘product_cat’ );
    if ( $categories && ! is_wp_error( $category ) )
    {
    foreach($categories as $category) :
    $children = get_categories( array (‘taxonomy’ => ‘product_cat’, ‘parent’ => $category->term_id ));
    if($children)
    echo $category->name.’ ‘;
    if ( count($children) == 0 )
    {
    //$cats_data[$category->term_id] = $category->name;
    echo ‘-‘.$category->name.”;
    }
    endforeach;
    }

    Above code shows category as –
    Apple ( Top category)
    -B200 M2 (Last level category)
    -B200 M3
    Cisco
    -Core i3 3.06GHz 21.5-inch Mid-2010
    -Core i3 3.1GHz 21.5-inch Late 2011
    -Eight Core 2.4GHz Server 2010
    iMac (second level category)
    ThinkServer

    But it should show like this –
    Apple ( Top category)
    – iMac (second level category)
    –B200 M2 (Last level category)
    –B200 M3
    Cisco
    -ThinkServer
    –Core i3 3.06GHz 21.5-inch Mid-2010
    –Core i3 3.1GHz 21.5-inch Late 2011
    –Eight Core 2.4GHz Server 2010

    I don’t understand why second level category is coming last, Do I need to add some kind of order to get the category array.
    In admin I have selected category same way I wanted to show, Let me know your suggestion.

    Thanks for your help

    Plugin Contributor Mike Jolley

    (@mikejolley)

    The order they output is down to the orderby arg: https://developer.www.ads-software.com/reference/functions/get_terms/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show related Categories Tree on product page’ is closed to new replies.