Merge pull request #4853 from scottbasgaard/master

Use 'include' parameter to show siblings/children of current category. Fixes #891
This commit is contained in:
Coen Jacobs 2014-02-19 17:09:21 +01:00
commit 8fdf4d94cf
2 changed files with 79 additions and 61 deletions

View File

@ -117,45 +117,42 @@ class WC_Product_Cat_List_Walker extends Walker {
if ( !$element ) if ( !$element )
return; return;
if ( ! $args[0]['show_children_only'] || ( $args[0]['show_children_only'] && ( $element->parent == 0 || $args[0]['current_category'] == $element->parent || in_array( $element->parent, $args[0]['current_category_ancestors'] ) ) ) ) { $id_field = $this->db_fields['id'];
$id_field = $this->db_fields['id']; //display this element
if ( is_array( $args[0] ) )
$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
//display this element $id = $element->$id_field;
if ( is_array( $args[0] ) )
$args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'start_el'), $cb_args);
$id = $element->$id_field; // descend only when the depth is right and there are children for this element
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
// descend only when the depth is right and there are children for this element foreach( $children_elements[ $id ] as $child ){
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
foreach( $children_elements[ $id ] as $child ){ if ( !isset($newlevel) ) {
$newlevel = true;
if ( !isset($newlevel) ) { //start the child delimiter
$newlevel = true; $cb_args = array_merge( array(&$output, $depth), $args);
//start the child delimiter call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
}
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
} }
unset( $children_elements[ $id ] ); $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
} }
unset( $children_elements[ $id ] );
if ( isset($newlevel) && $newlevel ){
//end the child delimiter
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
} }
if ( isset($newlevel) && $newlevel ){
//end the child delimiter
$cb_args = array_merge( array(&$output, $depth), $args);
call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
}
//end this element
$cb_args = array_merge( array(&$output, $element, $depth), $args);
call_user_func_array(array(&$this, 'end_el'), $cb_args);
} }
} }

View File

@ -75,6 +75,8 @@ class WC_Widget_Product_Categories extends WC_Widget {
public function widget( $args, $instance ) { public function widget( $args, $instance ) {
extract( $args ); extract( $args );
global $wp_query, $post, $woocommerce;
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
$c = ( isset( $instance['count'] ) && $instance['count'] ) ? '1' : '0'; $c = ( isset( $instance['count'] ) && $instance['count'] ) ? '1' : '0';
$h = $instance['hierarchical'] ? true : false; $h = $instance['hierarchical'] ? true : false;
@ -86,30 +88,70 @@ class WC_Widget_Product_Categories extends WC_Widget {
if ( $title ) if ( $title )
echo $before_title . $title . $after_title; echo $before_title . $title . $after_title;
$dropdown_args = array();
$cat_args = array( 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat' ); $cat_args = array( 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat' );
// Menu Order
$cat_args['menu_order'] = false; $cat_args['menu_order'] = false;
if ( $o == 'order' ) { if ( $o == 'order' ) {
$cat_args['menu_order'] = 'asc'; $cat_args['menu_order'] = 'asc';
} else { } else {
$cat_args['orderby'] = 'title'; $cat_args['orderby'] = 'title';
}
// Setup Current Category
$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' );
}
} }
// Show Siblings and Children Only
if ( $s && $this->current_cat ) {
if ( $this->current_cat->parent == 0 ) {
$category_children = $this->current_cat->term_id;
} else {
$category_children = $this->current_cat->parent;
}
$current_category_children = get_term_children( $category_children, 'product_cat' );
if ( $current_category_children ) {
$current_category_children = implode ( ", ", $current_category_children );
$dropdown_args['include'] = $current_category_children;
$cat_args['include'] = $current_category_children;
}
}
// Dropdown
if ( $d ) { if ( $d ) {
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258 $dropdown_defaults = array(
wc_product_dropdown_categories( array(
'show_counts' => $c, 'show_counts' => $c,
'hierarchical' => $h, 'hierarchical' => $h,
'show_uncategorized' => 0, 'show_uncategorized' => 0,
'orderby' => $o 'orderby' => $o
) ); );
$dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults );
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
wc_product_dropdown_categories( $dropdown_args );
?> ?>
<script type='text/javascript'> <script type='text/javascript'>
/* <![CDATA[ */ /* <![CDATA[ */
@ -124,34 +166,13 @@ class WC_Widget_Product_Categories extends WC_Widget {
</script> </script>
<?php <?php
// List
} else { } else {
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( WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' ); include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' );
$cat_args['walker'] = new WC_Product_Cat_List_Walker; $cat_args['walker'] = new WC_Product_Cat_List_Walker;
$cat_args['title_li'] = ''; $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['pad_counts'] = 1;
$cat_args['show_option_none'] = __('No product categories exist.', 'woocommerce' ); $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'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';