widget_cssclass = 'ccas_ajax_category_filter'; $this->widget_description = __( 'Show list of categories in widget and lets you search products based on category.', 'ajaxify-filters' ); $this->widget_id = 'ccas_ajax_product_categories'; $this->widget_name = __( 'AJAX Product Categories', 'ajaxify-filters' ); $this->settings = array( 'title' => array( 'type' => 'text', 'std' => __( 'Product Categories', 'ajaxify-filters' ), 'label' => __( 'Title', 'ajaxify-filters' ) ), 'orderby' => array( 'type' => 'select', 'std' => 'name', 'label' => __( 'Order by', 'ajaxify-filters' ), 'options' => array( 'order' => __( 'Category Order', 'ajaxify-filters' ), 'name' => __( 'Name', 'ajaxify-filters' ) ) ), 'count' => array( 'type' => 'checkbox', 'std' => 0, 'label' => __( 'Show product counts', 'ajaxify-filters' ) ), 'hierarchical' => array( 'type' => 'checkbox', 'std' => 1, 'label' => __( 'Show hierarchy', 'ajaxify-filters' ) ), 'show_children_only' => array( 'type' => 'checkbox', 'std' => 0, 'label' => __( 'Only show children of the current category', 'ajaxify-filters' ) ) ); parent::__construct(); } /** * Output widget. */ public function widget( $args, $instance ) { if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) { return; } global $wp_query, $post; $count = isset( $instance['count'] ) ? $instance['count'] : $this->settings['count']['std']; $hierarchical = isset( $instance['hierarchical'] ) ? $instance['hierarchical'] : $this->settings['hierarchical']['std']; $show_children_only = isset( $instance['show_children_only'] ) ? $instance['show_children_only'] : $this->settings['show_children_only']['std']; //$dropdown = isset( $instance['dropdown'] ) ? $instance['dropdown'] : $this->settings['dropdown']['std']; $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : $this->settings['orderby']['std']; //$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' ); // Menu Order $list_args['menu_order'] = false; if ( $orderby == 'order' ) { $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', 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 ( $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( 'product_cat', array( 'fields' => 'ids', 'parent' => $ancestor, 'hierarchical' => false, 'hide_empty' => false ) ); $siblings = array_merge( $siblings, $ancestor_siblings ); } } 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 ); } $dropdown_args['include'] = implode( ',', $include ); $list_args['include'] = implode( ',', $include ); if ( empty( $include ) ) { return; } } elseif ( $show_children_only ) { $dropdown_args['depth'] = 1; $dropdown_args['child_of'] = 0; $dropdown_args['hierarchical'] = 1; $list_args['depth'] = 1; $list_args['child_of'] = 0; $list_args['hierarchical'] = 1; } $this->widget_start( $args, $instance ); require_once( CED_CAF_PLUGIN_DIR_PATH. '/core/walkers/class-ced-caf-pro-cat-walker.php' ); $list_args['walker'] = new CED_CAF_Pro_Cat_Walker; $list_args['title_li'] = ''; $list_args['pad_counts'] = 1; $list_args['show_option_none'] = __('No product categories exist.', 'ajaxify-filters' ); $list_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : ''; $list_args['current_category_ancestors'] = $this->cat_ancestors; echo ''; $this->widget_end( $args ); } }