mercredi 28 janvier 2015

Display category and subcategory hierarchical


This is what I'm trying to achieve:


enter image description here


So the result should be like this:



<ul class="product-categories">
<li class="cat-item cat-item-18 current-cat">
<a href="/product-category/autumn-winter-2014-2015/">Autumn-Winter 2014-2015</a>
<ul class="product-subcategories">
<li class="cat-item cat-item-20"><a href="/product-category/autumn-winter-2014-2015/abrigo/">abrigo</a></li>
</ul>
</li>
<li class="cat-item cat-item-19"><a href="/product-category/silk-collection/">Silk collection</a></li>
</ul>


The problem is that right now, categories and subcategories are displaying the same way:


enter image description here



<ul class="product-categories">
<li class="cat-item cat-item-20"><a href="/product-category/autumn-winter-2014-2015/abrigo/">abrigo</a></li>
<li class="cat-item cat-item-18 current-cat"><a href="/product-category/autumn-winter-2014-2015/">Autumn-Winter 2014-2015</a></li>
<li class="cat-item cat-item-19"><a href="/product-category/silk-collection/">Silk collection</a></li>
</ul>


I'm using this code on woocommerce to output the menu:



global $wp_query, $post, $woocommerce;

$this->current_cat = false;
$this->cat_ancestors = array();

if ( is_tax('product_cat') ) {

$this->current_cat = $wp_query->queried_object;
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );

} elseif ( is_singular('product') ) {

$product_category = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent' ) );

if ( $product_category ) {
$this->current_cat = end( $product_category );
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
}

}

include_once( $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-list-walker.php' );

$cat_args['walker'] = new WC_Product_Cat_List_Walker;
$cat_args['title_li'] = '';
$cat_args['show_children_only'] = ( isset( $instance['show_children_only'] ) && $instance['show_children_only'] ) ? 1 : 0;
$cat_args['pad_counts'] = 1;
$cat_args['show_option_none'] = __('No product categories exist.', 'woocommerce' );
$cat_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';
$cat_args['current_category_ancestors'] = $this->cat_ancestors;

echo '<ul class="product-categories">';

wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $cat_args ) );

echo '</ul>';


Any help on how to do this?





Aucun commentaire:

Enregistrer un commentaire