Merge pull request #740 from helgatheviking/filtered_nav_widget

Filtered nav widget
This commit is contained in:
Mike Jolley 2012-03-03 11:57:11 -08:00
commit afecfdc28b
2 changed files with 130 additions and 85 deletions

View File

@ -27,16 +27,20 @@ class WC_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 );
// We're on a shop page so queue the woocommerce_get_products_in_view function
@ -119,6 +123,7 @@ class WC_Query {
);
set_transient( $transient_name, $unfiltered_product_ids );
}
// Store the variable

View File

@ -7,18 +7,22 @@
* @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 ) :
@ -26,28 +30,35 @@ function woocommerce_layered_nav_init() {
$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]) && $_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;
}
/**
* Layered Nav post filter
*/
function woocommerce_layered_nav_query( $filtered_posts ) {
global $_chosen_attributes, $woocommerce;
global $_chosen_attributes, $woocommerce, $wp_query;
if (sizeof($_chosen_attributes)>0) :
@ -112,7 +123,6 @@ function woocommerce_layered_nav_query( $filtered_posts ) {
endif;
endif;
endif;
return (array) $filtered_posts;
@ -136,22 +146,28 @@ 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;
global $_chosen_attributes, $woocommerce, $wp_query;
/* 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']);
@ -164,6 +180,12 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
'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){
@ -173,51 +195,24 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
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') {
$taxonomy_filter = str_replace('pa_', '', $taxonomy);
$found = true;
echo '<select id="dropdown_layered_nav_'.$taxonomy_filter.'">';
echo '<option value="">'. sprintf( __('Any %s', 'woocommerce'), $woocommerce->attribute_label( $taxonomy ) ) .'</option>';
foreach ($terms as $term) {
// Get count based on current view - uses transients
$transient_name = 'woocommerce_layered_nav_count_' . sanitize_key($taxonomy) . sanitize_key( $term->term_id );
if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) {
$_products_in_term = get_objects_in_term( $term->term_id, $taxonomy );
set_transient( $transient_name, $_products_in_term );
}
$option_is_set = (isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']));
// If this is an AND query, only show options with count > 0
if ($query_type=='and') {
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids));
if ($count>0) $found = true;
if ($count==0 && !$option_is_set) continue;
// If this is an OR query, show all options so search can be expanded
} else {
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids));
if ($count>0) $found = true;
}
/* KIA */
//if on a term page, skip that term in widget list
if( $term->term_id == get_queried_object()->term_id ) continue;
/* end KIA */
echo '<option value="'.$term->term_id.'" '.selected( (isset($_GET['filter_'.$taxonomy_filter])) ? $_GET['filter_'.$taxonomy_filter] : '' , $term->term_id, false).'>'.$term->name.'</option>';
}
echo '</select>';
@ -237,8 +232,18 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
// List display
echo "<ul>";
/* KIA */
// Force found when option is selected - do not force found on taxonomy attributes
if (!is_tax($_attributes_array) && is_array($_chosen_attributes) && array_key_exists($taxonomy, $_chosen_attributes) && $current_term !== $term->term_id) $found = true;
/* end KIA */
foreach ($terms as $term) {
/* KIA */
//if on a term page, skip that term in widget list
//if( $term->term_id == get_queried_object()->term_id ) continue;
/* end KIA */
// Get count based on current view - uses transients
$transient_name = 'woocommerce_layered_nav_count_' . sanitize_key($taxonomy) . sanitize_key( $term->term_id );
@ -249,14 +254,27 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
set_transient( $transient_name, $_products_in_term );
}
$option_is_set = (isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms']));
/* KIA */
//this is adding an additional chosen attribute on tax pages
//change: if there are no terms in array then don't add $taxonomy to array
if (is_array($_chosen_attributes[$taxonomy]['terms'])) {
$_chosen_attributes[$taxonomy]['terms'] = $_chosen_attributes[$taxonomy]['terms'];
}
/* end KIA */
$option_is_set = (isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])) ;
// If this is an AND query, only show options with count > 0
if ($query_type=='and') {
$count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids));
if ($count>0) $found = true;
/* KIA */
// skip the term for the current archive
if ( $current_term == $term->term_id ) continue ;
/* end KIA */
if ($count>0 && $current_term !== $term->term_id ) $found = true;
if ($count==0 && !$option_is_set) continue;
@ -289,12 +307,27 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
endif;
// All current filters
if ($_chosen_attributes) foreach ($_chosen_attributes as $name => $data) :
if ($name!==$taxonomy) :
$link = add_query_arg( strtolower(sanitize_title(str_replace('pa_', 'filter_', $name))), implode(',', $data['terms']), $link );
if ($_chosen_attributes) :
foreach ($_chosen_attributes as $name => $data) :
if ($name!==$taxonomy ) :
/* KIA */
//exclude query arg for current term archive term
while(in_array($current_term, $data['terms'])) {
$key = array_search($current_term, $data);
unset($data['terms'][$key]);
}
if(!empty($data['terms'])){
$link = add_query_arg( strtolower(sanitize_title(str_replace('pa_', 'filter_', $name))), implode(',', $data['terms']), $link );
}
/* end KIA */
if ($data['query_type']=='or') $link = add_query_arg( strtolower(sanitize_title(str_replace('pa_', 'query_type_', $name))), 'or', $link );
endif;
endforeach;
endforeach;
endif;
// Min/Max
if (isset($_GET['min_price'])) :
@ -362,6 +395,13 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget {
}
}
/** @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;