2011-08-10 17:11:11 +00:00
< ? php
/**
2011-09-04 00:02:44 +00:00
* Layered Navigation Widget and related functions
2011-08-10 17:11:11 +00:00
*
* @ package WooCommerce
* @ category Widgets
* @ author WooThemes
*/
2011-09-04 00:02:44 +00:00
2012-03-02 20:27:39 +00:00
if ( is_active_widget ( false , false , 'woocommerce_layered_nav' , 'true' ) && ! is_admin ()) :
add_action ( 'parse_query' , 'woocommerce_layered_nav_init' , 1 );
2011-12-09 21:47:12 +00:00
add_filter ( 'loop_shop_post_in' , 'woocommerce_layered_nav_query' );
2012-03-02 20:27:39 +00:00
2011-12-09 21:47:12 +00:00
endif ;
2012-03-02 20:27:39 +00:00
2011-09-04 00:02:44 +00:00
/**
* Layered Nav Init
*/
2012-03-02 20:27:39 +00:00
function woocommerce_layered_nav_init ( ) {
global $_chosen_attributes , $woocommerce , $_attributes_array ;
2011-09-04 00:02:44 +00:00
$_chosen_attributes = array ();
2012-03-02 20:27:39 +00:00
$_attributes_array = array ();
2011-09-04 00:02:44 +00:00
2012-03-02 20:27:39 +00:00
$attribute_taxonomies = $woocommerce -> attribute_taxonomies ;
if ( $attribute_taxonomies ) :
2011-09-04 00:02:44 +00:00
foreach ( $attribute_taxonomies as $tax ) :
$attribute = strtolower ( sanitize_title ( $tax -> attribute_name ));
2012-03-02 20:27:39 +00:00
$taxonomy = $woocommerce -> attribute_taxonomy_name ( $attribute );
/* KIA */
// create an array of product attribute taxonomies
$_attributes_array [] = $taxonomy ;
/* end KIA */
2011-09-04 00:02:44 +00:00
$name = 'filter_' . $attribute ;
2011-11-15 15:15:12 +00:00
$query_type_name = 'query_type_' . $attribute ;
2011-09-04 00:02:44 +00:00
2012-03-02 20:27:39 +00:00
if ( isset ( $_GET [ $name ]) && taxonomy_exists ( $taxonomy )) :
2011-11-15 15:15:12 +00:00
$_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 ;
2012-03-02 20:27:39 +00:00
/* KIA */
elseif ( $taxonomy == get_queried_object () -> taxonomy ) :
$_chosen_attributes [ $taxonomy ][ 'terms' ] = array ( get_queried_object () -> term_id );
/* end KIA */
endif ;
2011-09-04 00:02:44 +00:00
endforeach ;
2012-03-02 20:27:39 +00:00
endif ;
2011-09-04 00:02:44 +00:00
}
/**
* Layered Nav post filter
*/
2012-03-02 20:27:39 +00:00
function woocommerce_layered_nav_query ( $filtered_posts ) {
global $_chosen_attributes , $woocommerce , $wp_query ;
2011-09-04 00:02:44 +00:00
if ( sizeof ( $_chosen_attributes ) > 0 ) :
$matched_products = array ();
2011-11-15 15:15:12 +00:00
$filtered_attribute = false ;
2011-09-04 00:02:44 +00:00
2011-11-15 15:15:12 +00:00
foreach ( $_chosen_attributes as $attribute => $data ) :
$matched_products_from_attribute = array ();
$filtered = false ;
2012-03-02 20:27:39 +00:00
if ( sizeof ( $data [ 'terms' ]) > 0 ) :
2011-11-15 15:15:12 +00:00
foreach ( $data [ 'terms' ] as $value ) :
2011-09-04 00:02:44 +00:00
$posts = get_objects_in_term ( $value , $attribute );
2011-11-15 15:15:12 +00:00
// 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 ;
2011-09-04 00:02:44 +00:00
endif ;
$filtered = true ;
endforeach ;
endif ;
2011-11-15 15:15:12 +00:00
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 ;
2011-09-04 00:02:44 +00:00
endforeach ;
if ( $filtered ) :
2011-09-16 08:10:12 +00:00
$woocommerce -> query -> layered_nav_post__in = $matched_products ;
$woocommerce -> query -> layered_nav_post__in [] = 0 ;
2011-09-04 00:02:44 +00:00
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 ;
2012-03-02 20:27:39 +00:00
endif ;
2011-09-04 00:02:44 +00:00
return ( array ) $filtered_posts ;
}
/**
* Layered Nav Widget
*/
2011-08-10 17:11:11 +00:00
class WooCommerce_Widget_Layered_Nav extends WP_Widget {
2011-08-26 21:28:55 +00:00
/** Variables to setup the widget. */
var $woo_widget_cssclass ;
var $woo_widget_description ;
var $woo_widget_idbase ;
var $woo_widget_name ;
2011-08-10 17:11:11 +00:00
/** constructor */
function WooCommerce_Widget_Layered_Nav () {
2011-08-26 21:28:55 +00:00
/* Widget variable settings. */
$this -> woo_widget_cssclass = 'widget_layered_nav' ;
2012-01-05 11:31:22 +00:00
$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' );
2011-08-26 21:28:55 +00:00
$this -> woo_widget_idbase = 'woocommerce_layered_nav' ;
2012-03-02 20:27:39 +00:00
$this -> woo_widget_name = __ ( 'WooCommerce Layered Nav' , 'marcovaldo' );
2011-08-26 21:28:55 +00:00
/* Widget settings. */
$widget_ops = array ( 'classname' => $this -> woo_widget_cssclass , 'description' => $this -> woo_widget_description );
/* Create the widget. */
2012-03-02 20:27:39 +00:00
$this -> WP_Widget ( 'woocommerce_layered_nav' , $this -> woo_widget_name , $widget_ops );
2011-08-10 17:11:11 +00:00
}
2011-09-06 11:11:22 +00:00
/** @see WP_Widget */
2011-08-10 17:11:11 +00:00
function widget ( $args , $instance ) {
extract ( $args );
2012-03-02 20:27:39 +00:00
global $_chosen_attributes , $woocommerce ;
/* KIA */
global $_attributes_array ;
if ( ! is_tax ( 'product_cat' ) && ! is_post_type_archive ( 'product' ) && ! is_tax ( 'product_tag' ) && ! in_array ( get_query_var ( 'taxonomy' ), $_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*/
2011-11-15 15:15:12 +00:00
$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' ;
2012-02-07 12:09:23 +00:00
$display_type = ( isset ( $instance [ 'display_type' ])) ? $instance [ 'display_type' ] : 'list' ;
2011-08-10 17:11:11 +00:00
2012-03-02 20:27:39 +00:00
if ( ! taxonomy_exists ( $taxonomy )) return ;
2011-08-10 17:11:11 +00:00
$args = array (
'hide_empty' => '1'
);
2012-03-02 20:27:39 +00:00
$terms = get_terms ( $taxonomy , $args );
/* KIA */
//fix get_terms() that aren't sorting properly
usort ( $terms , array ( $this , 'kia_term_sort_function' ));
/* end KIA */
2011-08-10 17:11:11 +00:00
$count = count ( $terms );
2011-11-15 15:15:12 +00:00
2012-03-02 20:27:39 +00:00
if ( $count > 0 ){
2011-08-31 10:51:50 +00:00
2011-08-10 17:11:11 +00:00
$found = false ;
ob_start ();
echo $before_widget . $before_title . $title . $after_title ;
2012-03-02 20:27:39 +00:00
if ( $display_type == 'dropdown' ) {
2012-02-08 10:51:39 +00:00
$taxonomy_filter = str_replace ( 'pa_' , '' , $taxonomy );
2011-11-14 01:20:07 +00:00
2012-03-02 20:27:39 +00:00
$found = true ;
2012-02-08 10:51:39 +00:00
echo '<select id="dropdown_layered_nav_' . $taxonomy_filter . '">' ;
2011-11-14 01:20:07 +00:00
2012-02-08 10:51:39 +00:00
echo '<option value="">' . sprintf ( __ ( 'Any %s' , 'woocommerce' ), $woocommerce -> attribute_label ( $taxonomy ) ) . '</option>' ;
2012-02-07 12:09:23 +00:00
foreach ( $terms as $term ) {
2012-02-29 15:11:04 +00:00
2012-03-02 20:27:39 +00:00
/* KIA */
//if on a term page, skip that term in widget list
if ( $term -> term_id == get_queried_object () -> term_id ) continue ;
/* end KIA */
2012-02-29 15:11:04 +00:00
2012-02-08 10:51:39 +00:00
echo '<option value="' . $term -> term_id . '" ' . selected ( ( isset ( $_GET [ 'filter_' . $taxonomy_filter ])) ? $_GET [ 'filter_' . $taxonomy_filter ] : '' , $term -> term_id , false ) . '>' . $term -> name . '</option>' ;
2011-11-14 01:20:07 +00:00
}
2012-02-07 12:09:23 +00:00
echo '</select>' ;
2011-08-10 17:11:11 +00:00
2012-02-08 10:51:39 +00:00
$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 ();
});
" );
2012-02-07 12:09:23 +00:00
} else {
2012-03-02 20:27:39 +00:00
2012-02-07 12:09:23 +00:00
// List display
echo " <ul> " ;
2012-03-02 20:27:39 +00:00
// Force found when option is selected
if ( is_array ( $_chosen_attributes ) && array_key_exists ( $taxonomy , $_chosen_attributes )) $found = true ;
//print_r($_chosen_attributes);
2012-02-29 15:11:04 +00:00
2012-03-02 20:27:39 +00:00
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 */
2011-11-15 15:15:12 +00:00
2012-02-07 12:09:23 +00:00
// Get count based on current view - uses transients
$transient_name = 'woocommerce_layered_nav_count_' . sanitize_key ( $taxonomy ) . sanitize_key ( $term -> term_id );
2011-11-15 15:15:12 +00:00
2012-02-07 12:09:23 +00:00
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 );
}
2012-03-02 20:27:39 +00:00
/* 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 */
2012-02-07 12:09:23 +00:00
2012-03-02 20:27:39 +00:00
$option_is_set = ( isset ( $_chosen_attributes [ $taxonomy ]) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ][ 'terms' ]));
2012-02-07 12:09:23 +00:00
// 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 ;
if ( $count == 0 && ! $option_is_set ) continue ;
// If this is an OR query, show all options so search can be expanded
2012-03-02 20:27:39 +00:00
} else {
2012-02-07 12:09:23 +00:00
$count = sizeof ( array_intersect ( $_products_in_term , $woocommerce -> query -> unfiltered_product_ids ));
if ( $count > 0 ) $found = true ;
}
$class = '' ;
$arg = 'filter_' . strtolower ( sanitize_title ( $instance [ 'attribute' ]));
if ( isset ( $_GET [ $arg ])) $current_filter = explode ( ',' , $_GET [ $arg ]); else $current_filter = array ();
if ( ! is_array ( $current_filter )) $current_filter = array ();
if ( ! in_array ( $term -> term_id , $current_filter )) $current_filter [] = $term -> term_id ;
// Base Link decided by current page
if ( defined ( 'SHOP_IS_ON_FRONT' )) :
$link = home_url ();
elseif ( is_post_type_archive ( 'product' ) || is_page ( woocommerce_get_page_id ( 'shop' ) )) :
$link = get_post_type_archive_link ( 'product' );
else :
$link = get_term_link ( get_query_var ( 'term' ), get_query_var ( 'taxonomy' ) );
2012-03-02 20:27:39 +00:00
endif ;
2012-02-07 12:09:23 +00:00
// All current filters
2012-03-02 20:27:39 +00:00
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 ;
endif ;
2012-02-07 12:09:23 +00:00
// Min/Max
if ( isset ( $_GET [ 'min_price' ])) :
$link = add_query_arg ( 'min_price' , $_GET [ 'min_price' ], $link );
endif ;
if ( isset ( $_GET [ 'max_price' ])) :
$link = add_query_arg ( 'max_price' , $_GET [ 'max_price' ], $link );
2011-10-26 16:57:25 +00:00
endif ;
2011-11-15 15:15:12 +00:00
2012-02-07 12:09:23 +00:00
// Current Filter = this widget
if ( isset ( $_chosen_attributes [ $taxonomy ] ) && is_array ( $_chosen_attributes [ $taxonomy ][ 'terms' ]) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ][ 'terms' ])) :
$class = 'class="chosen"' ;
// Remove this term is $current_filter has more than 1 term filtered
2012-03-02 20:27:39 +00:00
if ( sizeof ( $current_filter ) > 1 ) :
2012-02-07 12:09:23 +00:00
$current_filter_without_this = array_diff ( $current_filter , array ( $term -> term_id ));
$link = add_query_arg ( $arg , implode ( ',' , $current_filter_without_this ), $link );
endif ;
else :
$link = add_query_arg ( $arg , implode ( ',' , $current_filter ), $link );
endif ;
// Search Arg
if ( get_search_query ()) :
$link = add_query_arg ( 's' , get_search_query (), $link );
endif ;
// Post Type Arg
if ( isset ( $_GET [ 'post_type' ])) :
$link = add_query_arg ( 'post_type' , $_GET [ 'post_type' ], $link );
endif ;
// Query type Arg
if ( $query_type == 'or' && ! ( sizeof ( $current_filter ) == 1 && isset ( $_chosen_attributes [ $taxonomy ][ 'terms' ] ) && is_array ( $_chosen_attributes [ $taxonomy ][ 'terms' ]) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ][ 'terms' ]) )) :
$link = add_query_arg ( 'query_type_' . strtolower ( sanitize_title ( $instance [ 'attribute' ])), 'or' , $link );
endif ;
echo '<li ' . $class . '>' ;
if ( $count > 0 || $option_is_set ) echo '<a href="' . $link . '">' ; else echo '<span>' ;
echo $term -> name ;
if ( $count > 0 || $option_is_set ) echo '</a>' ; else echo '</span>' ;
echo ' <small class="count">' . $count . '</small></li>' ;
}
2011-08-10 17:11:11 +00:00
2012-02-07 12:09:23 +00:00
echo " </ul> " ;
2011-08-10 17:11:11 +00:00
2012-02-07 12:09:23 +00:00
} // End display type conditional
2011-08-10 17:11:11 +00:00
echo $after_widget ;
if ( ! $found ) :
2012-03-02 20:27:39 +00:00
ob_clean ();
2011-08-10 17:11:11 +00:00
return ;
else :
2012-03-02 20:27:39 +00:00
$widget = ob_get_clean ();
echo $widget ;
2011-08-10 17:11:11 +00:00
endif ;
}
}
2012-03-02 20:27:39 +00:00
/** @ 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 );
}
2011-09-06 11:11:22 +00:00
/** @see WP_Widget->update */
2011-08-10 17:11:11 +00:00
function update ( $new_instance , $old_instance ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
if ( ! isset ( $new_instance [ 'title' ]) || empty ( $new_instance [ 'title' ])) $new_instance [ 'title' ] = $woocommerce -> attribute_label ( $new_instance [ 'attribute' ]);
2011-08-10 17:11:11 +00:00
$instance [ 'title' ] = strip_tags ( stripslashes ( $new_instance [ 'title' ]));
$instance [ 'attribute' ] = stripslashes ( $new_instance [ 'attribute' ]);
2011-11-15 15:15:12 +00:00
$instance [ 'query_type' ] = stripslashes ( $new_instance [ 'query_type' ]);
2012-02-07 12:09:23 +00:00
$instance [ 'display_type' ] = stripslashes ( $new_instance [ 'display_type' ]);
2011-08-10 17:11:11 +00:00
return $instance ;
}
2011-09-06 11:11:22 +00:00
/** @see WP_Widget->form */
2011-08-10 17:11:11 +00:00
function form ( $instance ) {
2011-11-14 01:20:07 +00:00
global $woocommerce ;
2011-11-15 15:15:12 +00:00
if ( ! isset ( $instance [ 'query_type' ])) $instance [ 'query_type' ] = 'and' ;
2012-02-07 12:09:23 +00:00
if ( ! isset ( $instance [ 'display_type' ])) $instance [ 'display_type' ] = 'list' ;
2011-08-10 17:11:11 +00:00
?>
2012-01-05 11:31:22 +00:00
< p >< label for = " <?php echo $this->get_field_id ('title'); ?> " >< ? php _e ( 'Title:' , 'woocommerce' ) ?> </label>
2011-09-19 06:01:26 +00:00
< input type = " text " class = " widefat " id = " <?php echo esc_attr( $this->get_field_id ('title') ); ?> " name = " <?php echo esc_attr( $this->get_field_name ('title') ); ?> " value = " <?php if (isset ( $instance['title'] )) { echo esc_attr( $instance['title'] );} ?> " /></ p >
2011-08-10 17:11:11 +00:00
2012-01-05 11:31:22 +00:00
< p >< label for = " <?php echo $this->get_field_id ('attribute'); ?> " >< ? php _e ( 'Attribute:' , 'woocommerce' ) ?> </label>
2011-09-19 06:01:26 +00:00
< select id = " <?php echo esc_attr( $this->get_field_id ('attribute') ); ?> " name = " <?php echo esc_attr( $this->get_field_name ('attribute') ); ?> " >
2011-08-10 17:11:11 +00:00
< ? php
2011-09-06 11:11:22 +00:00
$attribute_taxonomies = $woocommerce -> get_attribute_taxonomies ();
2011-08-10 17:11:11 +00:00
if ( $attribute_taxonomies ) :
foreach ( $attribute_taxonomies as $tax ) :
2011-09-11 13:28:15 +00:00
if ( taxonomy_exists ( $woocommerce -> attribute_taxonomy_name ( $tax -> attribute_name ))) :
2011-08-10 17:11:11 +00:00
echo '<option value="' . $tax -> attribute_name . '" ' ;
if ( isset ( $instance [ 'attribute' ]) && $instance [ 'attribute' ] == $tax -> attribute_name ) :
echo 'selected="selected"' ;
endif ;
echo '>' . $tax -> attribute_name . '</option>' ;
endif ;
endforeach ;
endif ;
?>
2011-11-15 15:15:12 +00:00
</ select ></ p >
2012-02-07 12:09:23 +00:00
< p >< label for = " <?php echo $this->get_field_id ('display_type'); ?> " >< ? php _e ( 'Display Type:' , 'woocommerce' ) ?> </label>
< select id = " <?php echo esc_attr( $this->get_field_id ('display_type') ); ?> " name = " <?php echo esc_attr( $this->get_field_name ('display_type') ); ?> " >
< option value = " list " < ? php selected ( $instance [ 'display_type' ], 'list' ); ?> ><?php _e('List', 'woocommerce'); ?></option>
< option value = " dropdown " < ? php selected ( $instance [ 'display_type' ], 'dropdown' ); ?> ><?php _e('Dropdown', 'woocommerce'); ?></option>
</ select ></ p >
2012-01-05 11:31:22 +00:00
< p >< label for = " <?php echo $this->get_field_id ('query_type'); ?> " >< ? php _e ( 'Query Type:' , 'woocommerce' ) ?> </label>
2011-11-15 15:15:12 +00:00
< select id = " <?php echo esc_attr( $this->get_field_id ('query_type') ); ?> " name = " <?php echo esc_attr( $this->get_field_name ('query_type') ); ?> " >
2012-01-05 11:31:22 +00:00
< option value = " and " < ? php selected ( $instance [ 'query_type' ], 'and' ); ?> ><?php _e('AND', 'woocommerce'); ?></option>
< option value = " or " < ? php selected ( $instance [ 'query_type' ], 'or' ); ?> ><?php _e('OR', 'woocommerce'); ?></option>
2011-11-15 15:15:12 +00:00
</ select ></ p >
2011-08-10 17:11:11 +00:00
< ? php
}
} // class WooCommerce_Widget_Layered_Nav