diff --git a/classes/class-wc-query.php b/classes/class-wc-query.php index f400b6d8315..208fe0b68c2 100644 --- a/classes/class-wc-query.php +++ b/classes/class-wc-query.php @@ -26,16 +26,20 @@ class WC_Query { * Hook into pre_get_posts to do the main product query */ function pre_get_posts( $q ) { - - // Only apply to product categories, the product post archive, the shop page, and product tags + + global $_attributes_array; + + // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies if ( ( !$q->is_main_query() ) || ( - !$q->is_tax( 'product_cat' ) + !$q->is_post_type_archive( 'product' ) + && !$q->is_tax( 'product_cat' ) && !$q->is_tax( 'product_tag' ) - && !$q->is_post_type_archive( 'product' ) - ) + && !$q->is_tax($_attributes_array) + ) ) return; + $this->product_query( $q ); @@ -119,6 +123,7 @@ class WC_Query { ); set_transient( $transient_name, $unfiltered_product_ids ); + } // Store the variable diff --git a/widgets/widget-layered_nav.php b/widgets/widget-layered_nav.php index 4c2ead010fc..d159d9344bb 100644 --- a/widgets/widget-layered_nav.php +++ b/widgets/widget-layered_nav.php @@ -7,48 +7,59 @@ * @author WooThemes */ -if (is_active_widget( false, false, 'layered_nav', 'true' ) && !is_admin()) : - add_action('init', 'woocommerce_layered_nav_init', 1); + +if (is_active_widget( false, false, 'woocommerce_layered_nav', 'true' ) && !is_admin()) : + add_action('parse_query', 'woocommerce_layered_nav_init', 1); add_filter('loop_shop_post_in', 'woocommerce_layered_nav_query'); + endif; + /** * Layered Nav Init */ -function woocommerce_layered_nav_init() { - global $_chosen_attributes, $woocommerce; +function woocommerce_layered_nav_init( ) { + global $_chosen_attributes, $woocommerce, $_attributes_array; $_chosen_attributes = array(); + $_attributes_array = array(); - $attribute_taxonomies = $woocommerce->attribute_taxonomies; - if ( $attribute_taxonomies ) : + $attribute_taxonomies = $woocommerce->attribute_taxonomies; + if ( $attribute_taxonomies ) : foreach ($attribute_taxonomies as $tax) : $attribute = strtolower(sanitize_title($tax->attribute_name)); - $taxonomy = $woocommerce->attribute_taxonomy_name($attribute); + $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]) && $_GET[$name] && taxonomy_exists($taxonomy)) : + 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; - endif; - + /* KIA */ + elseif ( $taxonomy == get_queried_object()->taxonomy ) : + $_chosen_attributes[$taxonomy]['terms'] = array( get_queried_object()->term_id ); + /* end KIA */ + endif; endforeach; - endif; - + endif; } /** * Layered Nav post filter */ -function woocommerce_layered_nav_query( $filtered_posts ) { - global $_chosen_attributes, $woocommerce; - +function woocommerce_layered_nav_query( $filtered_posts ) { + global $_chosen_attributes, $woocommerce, $wp_query; + if (sizeof($_chosen_attributes)>0) : $matched_products = array(); @@ -59,7 +70,7 @@ function woocommerce_layered_nav_query( $filtered_posts ) { $matched_products_from_attribute = array(); $filtered = false; - if (sizeof($data['terms'])>0) : + if (sizeof($data['terms'])>0) : foreach ($data['terms'] as $value) : $posts = get_objects_in_term( $value, $attribute ); @@ -112,8 +123,7 @@ function woocommerce_layered_nav_query( $filtered_posts ) { endif; endif; - - endif; + endif; return (array) $filtered_posts; } @@ -136,88 +146,73 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { $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', 'woocommerce' ); + $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('layered_nav', $this->woo_widget_name, $widget_ops); + $this->WP_Widget('woocommerce_layered_nav', $this->woo_widget_name, $widget_ops); } /** @see WP_Widget */ function widget( $args, $instance ) { extract($args); - - if (!is_tax( 'product_cat' ) && !is_post_type_archive('product') && !is_tax( 'product_tag' )) return; - - global $_chosen_attributes, $woocommerce, $wp_query; + 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; + if (!taxonomy_exists($taxonomy)) return; $args = array( 'hide_empty' => '1' ); - $terms = get_terms( $taxonomy, $args ); + $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){ + if($count > 0){ $found = false; ob_start(); echo $before_widget . $before_title . $title . $after_title; - // Force found when option is selected - if (is_array($_chosen_attributes) && array_key_exists($taxonomy, $_chosen_attributes)) $found = true; - - if ($display_type=='dropdown') { + if ($display_type=='dropdown') { $taxonomy_filter = str_replace('pa_', '', $taxonomy); + $found = true; + echo ''; @@ -233,11 +228,21 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { "); } else { - + // List display echo "