Add max depth option to widget

Closes #16132
This commit is contained in:
Mike Jolley 2017-08-25 12:49:40 +01:00
parent ff5228e944
commit 55d75cdcb7
1 changed files with 55 additions and 57 deletions

View File

@ -77,6 +77,11 @@ class WC_Widget_Product_Categories extends WC_Widget {
'std' => 0,
'label' => __( 'Hide empty categories', 'woocommerce' ),
),
'max_depth' => array(
'type' => 'text',
'std' => '',
'label' => __( 'Maximum depth', 'woocommerce' ),
),
);
parent::__construct();
@ -86,7 +91,6 @@ class WC_Widget_Product_Categories extends WC_Widget {
* Output widget.
*
* @see WP_Widget
*
* @param array $args
* @param array $instance
*/
@ -101,26 +105,26 @@ class WC_Widget_Product_Categories extends WC_Widget {
$hide_empty = isset( $instance['hide_empty'] ) ? $instance['hide_empty'] : $this->settings['hide_empty']['std'];
$dropdown_args = array( 'hide_empty' => $hide_empty );
$list_args = array( 'show_count' => $count, 'hierarchical' => $hierarchical, 'taxonomy' => 'product_cat', 'hide_empty' => $hide_empty );
$max_depth = absint( isset( $instance['max_depth'] ) ? $instance['max_depth'] : $this->settings['max_depth']['std']);
// Menu Order
$list_args['menu_order'] = false;
$dropdown_args['depth'] = $max_depth;
$list_args['depth'] = $max_depth;
if ( 'order' === $orderby ) {
$list_args['menu_order'] = 'asc';
} else {
$list_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', apply_filters( 'woocommerce_product_categories_widget_product_terms_args', array( 'orderby' => 'parent' ) ) );
if ( ! empty( $product_category ) ) {
@ -129,56 +133,59 @@ class WC_Widget_Product_Categories extends WC_Widget {
}
}
// Show Siblings and Children Only
// Show Siblings and Children Only.
if ( $show_children_only && $this->current_cat ) {
// Top level is needed
$top_level = get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => 0,
'hierarchical' => true,
'hide_empty' => false,
)
);
// Direct children are wanted
$direct_children = get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $this->current_cat->term_id,
'hierarchical' => true,
'hide_empty' => false,
)
);
// Gather siblings of ancestors
$siblings = array();
if ( $this->cat_ancestors ) {
foreach ( $this->cat_ancestors as $ancestor ) {
$ancestor_siblings = get_terms(
if ( $hierarchical ) {
$include = array_merge(
$this->cat_ancestors,
array( $this->current_cat->term_id ),
get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $ancestor,
'hierarchical' => false,
'parent' => 0,
'hierarchical' => true,
'hide_empty' => false,
)
);
$siblings = array_merge( $siblings, $ancestor_siblings );
),
get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $this->current_cat->term_id,
'hierarchical' => true,
'hide_empty' => false,
)
)
);
// Gather siblings of ancestors.
if ( $this->cat_ancestors ) {
foreach ( $this->cat_ancestors as $ancestor ) {
$include = array_merge( $include, get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $ancestor,
'hierarchical' => false,
'hide_empty' => false,
)
) );
}
}
}
if ( $hierarchical ) {
$include = array_merge( $top_level, $this->cat_ancestors, $siblings, $direct_children, array( $this->current_cat->term_id ) );
} else {
$include = array_merge( $direct_children );
// Direct children.
$include = get_terms(
'product_cat',
array(
'fields' => 'ids',
'parent' => $this->current_cat->term_id,
'hierarchical' => true,
'hide_empty' => false,
)
);
}
$dropdown_args['include'] = implode( ',', $include );
$list_args['include'] = implode( ',', $include );
$dropdown_args['include'] = $list_args['include'] = implode( ',', $include );
if ( empty( $include ) ) {
return;
@ -194,20 +201,14 @@ class WC_Widget_Product_Categories extends WC_Widget {
$this->widget_start( $args, $instance );
// Dropdown
if ( $dropdown ) {
$dropdown_defaults = array(
wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', wp_parse_args( $dropdown_args, array(
'show_count' => $count,
'hierarchical' => $hierarchical,
'show_uncategorized' => 0,
'orderby' => $orderby,
'selected' => $this->current_cat ? $this->current_cat->slug : '',
);
$dropdown_args = wp_parse_args( $dropdown_args, $dropdown_defaults );
// Stuck with this until a fix for https://core.trac.wordpress.org/ticket/13258
wc_product_dropdown_categories( apply_filters( 'woocommerce_product_categories_widget_dropdown_args', $dropdown_args ) );
) ) ) );
wc_enqueue_js( "
jQuery( '.dropdown_product_cat' ).change( function() {
if ( jQuery(this).val() != '' ) {
@ -222,17 +223,14 @@ class WC_Widget_Product_Categories extends WC_Widget {
}
});
" );
// List
} else {
include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-list-walker.php' );
$list_args['walker'] = new WC_Product_Cat_List_Walker;
$list_args['title_li'] = '';
$list_args['pad_counts'] = 1;
$list_args['show_option_none'] = __( 'No product categories exist.', 'woocommerce' );
$list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';
$list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id: '';
$list_args['current_category_ancestors'] = $this->cat_ancestors;
echo '<ul class="product-categories">';