woo_widget_cssclass = 'widget_product_categories';
$this->woo_widget_description = __( 'A list or dropdown of product categories.', 'woocommerce' );
$this->woo_widget_idbase = 'woocommerce_product_categories';
$this->woo_widget_name = __('WooCommerce Product Categories', 'woocommerce' );
/* Widget settings. */
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
/* Create the widget. */
$this->WP_Widget('product_categories', $this->woo_widget_name, $widget_ops);
}
/** @see WP_Widget */
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Product Categories', 'woocommerce' ) : $instance['title'], $instance, $this->id_base);
$c = $instance['count'] ? '1' : '0';
$h = $instance['hierarchical'] ? '1' : '0';
$s = (isset($instance['show_children_only']) && $instance['show_children_only']) ? '1' : '0';
$d = $instance['dropdown'] ? '1' : '0';
$o = isset($instance['orderby']) ? $instance['orderby'] : 'order';
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
$cat_args = array('show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat');
if ( $o == 'order' ) {
$cat_args['menu_order'] = 'asc';
} else {
$cat_args['orderby'] = $o;
}
if ( $d ) {
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
woocommerce_product_dropdown_categories( $c, $h );
?>
current_cat = $wp_query->queried_object;
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
else :
$this->current_cat = false;
$this->cat_ancestors = array();
endif;
echo '
';
foreach ($cats as $cat) :
// Only show top level for now
if ($cat->parent) continue;
echo '- '.$cat->name.'';
if ($c) echo ' ('.$cat->count.')';
if ( is_tax('product_cat', $cat->slug) || (in_array( $cat->term_id, $this->cat_ancestors ))) :
$children = $this->get_children_cats( $cat->term_id );
$this->output_children_cats( $children, $c );
endif;
echo '
';
endforeach;
echo '
';
} else {
echo '';
$cat_args['title_li'] = '';
wp_list_categories(apply_filters('woocommerce_product_categories_widget_args', $cat_args));
echo '
';
}
echo $after_widget;
}
function get_children_cats( $parent ) {
$cat_args = array();
$cat_args['title_li'] = '';
$cat_args['hierarchical'] = 1;
$cat_args['child_of'] = 0;
$cat_args['pad_counts'] = 1;
$cat_args['parent'] = $parent;
return get_terms( 'product_cat', apply_filters('woocommerce_product_categories_widget_subcat_args', $cat_args) );
}
function output_children_cats( $children, $c ) {
echo '';
foreach ($children as $child) {
echo '- '.$child->name.'';
if ($c) echo ' ('.$child->count.')';
if ( is_tax('product_cat', $child->slug) || (in_array( $child->term_id, $this->cat_ancestors ))) {
$children_children = $this->get_children_cats( $child->term_id );
if ($children_children) $this->output_children_cats( $children_children, $c );
}
echo '
';
}
echo '
';
}
/** @see WP_Widget->update */
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['orderby'] = strip_tags($new_instance['orderby']);
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
$instance['show_children_only'] = !empty($new_instance['show_children_only']) ? 1 : 0;
return $instance;
}
/** @see WP_Widget->form */
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = esc_attr( $instance['title'] );
$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'order';
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
$show_children_only = isset( $instance['show_children_only'] ) ? (bool) $instance['show_children_only'] : false;
?>
/>
/>
/>
/>