widget_cssclass = 'woocommerce widget_layered_nav'; $this->widget_description = __( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.', 'woocommerce' ); $this->widget_id = 'woocommerce_layered_nav'; $this->widget_name = __( 'WooCommerce Layered Nav', 'woocommerce' ); parent::__construct(); } /** * update function. * * @see WP_Widget->update * * @param array $new_instance * @param array $old_instance * * @return array */ public function update( $new_instance, $old_instance ) { $this->init_settings(); return parent::update( $new_instance, $old_instance ); } /** * form function. * * @see WP_Widget->form * * @param array $instance * * @return void */ public function form( $instance ) { $this->init_settings(); parent::form( $instance ); } /** * Init settings after post types are registered * * @return void */ public function init_settings() { $attribute_array = array(); $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) { foreach ( $attribute_taxonomies as $tax ) { if ( taxonomy_exists( wc_attribute_taxonomy_name( $tax->attribute_name ) ) ) { $attribute_array[ $tax->attribute_name ] = $tax->attribute_name; } } } $this->settings = array( 'title' => array( 'type' => 'text', 'std' => __( 'Filter by', 'woocommerce' ), 'label' => __( 'Title', 'woocommerce' ) ), 'attribute' => array( 'type' => 'select', 'std' => '', 'label' => __( 'Attribute', 'woocommerce' ), 'options' => $attribute_array ), 'display_type' => array( 'type' => 'select', 'std' => 'list', 'label' => __( 'Display type', 'woocommerce' ), 'options' => array( 'list' => __( 'List', 'woocommerce' ), 'dropdown' => __( 'Dropdown', 'woocommerce' ) ) ), 'query_type' => array( 'type' => 'select', 'std' => 'and', 'label' => __( 'Query type', 'woocommerce' ), 'options' => array( 'and' => __( 'AND', 'woocommerce' ), 'or' => __( 'OR', 'woocommerce' ) ) ), ); } /** * widget function. * * @see WP_Widget * * @param array $args * @param array $instance * * @return void */ public function widget( $args, $instance ) { global $_chosen_attributes; if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) { return; } $current_term = is_tax() ? get_queried_object()->term_id : ''; $current_tax = is_tax() ? get_queried_object()->taxonomy : ''; $taxonomy = isset( $instance['attribute'] ) ? wc_attribute_taxonomy_name( $instance['attribute'] ) : $this->settings['attribute']['std']; $query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : $this->settings['query_type']['std']; $display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : $this->settings['display_type']['std']; if ( ! taxonomy_exists( $taxonomy ) ) { return; } $get_terms_args = array( 'hide_empty' => '1' ); $orderby = wc_attribute_orderby( $taxonomy ); switch ( $orderby ) { case 'name' : $get_terms_args['orderby'] = 'name'; $get_terms_args['menu_order'] = false; break; case 'id' : $get_terms_args['orderby'] = 'id'; $get_terms_args['order'] = 'ASC'; $get_terms_args['menu_order'] = false; break; case 'menu_order' : $get_terms_args['menu_order'] = 'ASC'; break; } $terms = get_terms( $taxonomy, $get_terms_args ); if ( 0 < count( $terms ) ) { ob_start(); $found = false; $this->widget_start( $args, $instance ); // Force found when option is selected - do not force found on taxonomy attributes if ( ! is_tax() && is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) { $found = true; } if ( 'dropdown' == $display_type ) { // skip when viewing the taxonomy if ( $current_tax && $taxonomy == $current_tax ) { $found = false; } else { $taxonomy_filter = str_replace( 'pa_', '', $taxonomy ); $found = false; echo ''; wc_enqueue_js( " jQuery('.dropdown_layered_nav_$taxonomy_filter').change(function(){ location.href = '" . esc_url_raw( preg_replace( '%\/page/[0-9]+%', '', add_query_arg( 'filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy_filter ) ) ) ) ) . "&filter_$taxonomy_filter=' + jQuery(this).val(); }); " ); } } else { // List display echo ''; } // End display type conditional $this->widget_end( $args ); if ( ! $found ) { ob_end_clean(); } else { echo ob_get_clean(); } } } }