attribute_taxonomies; if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : $attribute = strtolower(sanitize_title($tax->attribute_name)); $taxonomy = $woocommerce->attribute_taxonomy_name($attribute); /* KIA */ // create an array of product attribute taxonomies $_attributes_array[] = $taxonomy; /* end KIA */ $name = 'filter_' . $attribute; $query_type_name = 'query_type_' . $attribute; if (isset($_GET[$name]) && taxonomy_exists($taxonomy)) : $_chosen_attributes[$taxonomy]['terms'] = explode(',', $_GET[$name] ); if (isset($_GET[$query_type_name]) && $_GET[$query_type_name]=='or') : $_chosen_attributes[$taxonomy]['query_type'] = 'or'; else : $_chosen_attributes[$taxonomy]['query_type'] = 'and'; endif; /* KIA */ elseif ( $taxonomy == get_queried_object()->taxonomy ) : $_chosen_attributes[$taxonomy]['terms'] = array( get_queried_object()->term_id ); /* end KIA */ endif; endforeach; endif; } /** * Layered Nav post filter */ function woocommerce_layered_nav_query( $filtered_posts ) { global $_chosen_attributes, $woocommerce, $wp_query; if (sizeof($_chosen_attributes)>0) : $matched_products = array(); $filtered_attribute = false; foreach ($_chosen_attributes as $attribute => $data) : $matched_products_from_attribute = array(); $filtered = false; if (sizeof($data['terms'])>0) : foreach ($data['terms'] as $value) : $posts = get_objects_in_term( $value, $attribute ); // AND or OR if ($data['query_type']=='or') : if (!is_wp_error($posts) && (sizeof($matched_products_from_attribute)>0 || $filtered)) : $matched_products_from_attribute = array_merge($posts, $matched_products_from_attribute); elseif (!is_wp_error($posts)) : $matched_products_from_attribute = $posts; endif; else : if (!is_wp_error($posts) && (sizeof($matched_products_from_attribute)>0 || $filtered)) : $matched_products_from_attribute = array_intersect($posts, $matched_products_from_attribute); elseif (!is_wp_error($posts)) : $matched_products_from_attribute = $posts; endif; endif; $filtered = true; endforeach; endif; if (sizeof($matched_products)>0 || $filtered_attribute) : $matched_products = array_intersect($matched_products_from_attribute, $matched_products); else : $matched_products = $matched_products_from_attribute; endif; $filtered_attribute = true; endforeach; if ($filtered) : $woocommerce->query->layered_nav_post__in = $matched_products; $woocommerce->query->layered_nav_post__in[] = 0; if (sizeof($filtered_posts)==0) : $filtered_posts = $matched_products; $filtered_posts[] = 0; else : $filtered_posts = array_intersect($filtered_posts, $matched_products); $filtered_posts[] = 0; endif; endif; endif; return (array) $filtered_posts; } /** * Layered Nav Widget */ class WooCommerce_Widget_Layered_Nav extends WP_Widget { /** Variables to setup the widget. */ var $woo_widget_cssclass; var $woo_widget_description; var $woo_widget_idbase; var $woo_widget_name; /** constructor */ function WooCommerce_Widget_Layered_Nav() { /* Widget variable settings. */ $this->woo_widget_cssclass = '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', 'marcovaldo' ); /* 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); } /** @see WP_Widget */ function widget( $args, $instance ) { extract($args); global $_chosen_attributes, $woocommerce; /* KIA */ global $_attributes_array; if ( !is_tax( 'product_cat' ) && !is_post_type_archive('product') && !is_tax( 'product_tag' ) && !is_tax($_attributes_array ) ) return; $current_term = (is_tax($_attributes_array)) ? get_queried_object()->term_id : NULL; $current_tax = (is_tax($_attributes_array)) ? get_queried_object()->taxonomy: NULL; /* end KIA*/ $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; $args = array( 'hide_empty' => '1' ); $terms = get_terms( $taxonomy, $args ); /* KIA */ //fix get_terms() that aren't sorting properly usort($terms, array($this, 'kia_term_sort_function')); /* end KIA */ $count = count($terms); if($count > 0){ $found = false; ob_start(); echo $before_widget . $before_title . $title . $after_title; if ($display_type=='dropdown') { $taxonomy_filter = str_replace('pa_', '', $taxonomy); $found = true; echo ''; $woocommerce->add_inline_js(" jQuery('#dropdown_layered_nav_$taxonomy_filter').change(function(){ location.href = '".home_url(remove_query_arg('filter_' . $taxonomy_filter, add_query_arg('filtering', '1')))."&filter_$taxonomy_filter=' + jQuery('#dropdown_layered_nav_$taxonomy_filter').val(); }); "); } else { // List display echo ""; } // End display type conditional echo $after_widget; if (!$found) : ob_clean(); return; else : $widget = ob_get_clean(); echo $widget; endif; } } /** @see WP_Widget->kia_term_sort used by usort() to fix get_terms() that aren't sorting properly */ function kia_term_sort_function($a, $b) { return strcmp($a->name, $b->name); } /** @see WP_Widget->update */ function update( $new_instance, $old_instance ) { global $woocommerce; if (!isset($new_instance['title']) || 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; } /** @see WP_Widget->form */ function form( $instance ) { global $woocommerce; if (!isset($instance['query_type'])) $instance['query_type'] = 'and'; if (!isset($instance['display_type'])) $instance['display_type'] = 'list'; ?>