woo_widget_cssclass = 'woocommerce widget_layered_nav'; $this->woo_widget_description = __( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.', 'woocommerce' ); $this->woo_widget_idbase = 'woocommerce_layered_nav'; $this->woo_widget_name = __( 'WooCommerce Layered Nav', 'woocommerce' ); /* Widget settings. */ $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description ); /* Create the widget. */ $this->WP_Widget( 'woocommerce_layered_nav', $this->woo_widget_name, $widget_ops ); } /** * widget function. * * @see WP_Widget * @access public * @param array $args * @param array $instance * @return void */ function widget( $args, $instance ) { global $_chosen_attributes, $woocommerce, $_attributes_array; extract( $args ); if ( ! is_post_type_archive( 'product' ) && ! is_tax( array_merge( $_attributes_array, array( 'product_cat', 'product_tag' ) ) ) ) return; $current_term = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->term_id : ''; $current_tax = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->taxonomy : ''; $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); $taxonomy = $woocommerce->attribute_taxonomy_name($instance['attribute']); $query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : 'and'; $display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : 'list'; if ( ! taxonomy_exists( $taxonomy ) ) return; $terms = get_terms( $taxonomy, array( 'hide_empty' => '1' ) ); if ( count( $terms ) > 0 ) { ob_start(); $found = false; echo $before_widget . $before_title . $title . $after_title; // Force found when option is selected - do not force found on taxonomy attributes if ( ! $_attributes_array || ! is_tax( $_attributes_array ) ) if ( is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) $found = true; if ( $display_type == 'dropdown' ) { // skip when viewing the taxonomy if ( $current_tax && $taxonomy == $current_tax ) { $found = false; } else { $taxonomy_filter = str_replace( 'pa_', '', $taxonomy ); $found = false; echo ''; $woocommerce->add_inline_js(" jQuery('#dropdown_layered_nav_$taxonomy_filter').change(function(){ location.href = '" . preg_replace( '%\/page/[0-9]+%', '', add_query_arg('filtering', '1', remove_query_arg( array( 'page', 'filter_' . $taxonomy_filter ) ) ) ) . "&filter_$taxonomy_filter=' + jQuery('#dropdown_layered_nav_$taxonomy_filter').val(); }); "); } } else { // List display echo ""; } // End display type conditional echo $after_widget; if ( ! $found ) ob_end_clean(); else echo ob_get_clean(); } } /** * update function. * * @see WP_Widget->update * @access public * @param array $new_instance * @param array $old_instance * @return array */ function update( $new_instance, $old_instance ) { global $woocommerce; if ( empty( $new_instance['title'] ) ) $new_instance['title'] = $woocommerce->attribute_label( $new_instance['attribute'] ); $instance['title'] = strip_tags( stripslashes($new_instance['title'] ) ); $instance['attribute'] = stripslashes( $new_instance['attribute'] ); $instance['query_type'] = stripslashes( $new_instance['query_type'] ); $instance['display_type'] = stripslashes( $new_instance['display_type'] ); return $instance; } /** * form function. * * @see WP_Widget->form * @access public * @param array $instance * @return void */ function form( $instance ) { global $woocommerce; if ( ! isset( $instance['query_type'] ) ) $instance['query_type'] = 'and'; if ( ! isset( $instance['display_type'] ) ) $instance['display_type'] = 'list'; ?>