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
/**
* Layered Nav Init
*/
add_action ( 'init' , 'woocommerce_layered_nav_init' , 1 );
function woocommerce_layered_nav_init () {
2011-09-06 11:11:22 +00:00
global $_chosen_attributes , $wpdb , $woocommerce ;
2011-09-04 00:02:44 +00:00
$_chosen_attributes = array ();
2011-09-06 11:11:22 +00:00
$attribute_taxonomies = $woocommerce -> attribute_taxonomies ;
2011-09-04 00:02:44 +00:00
if ( $attribute_taxonomies ) :
foreach ( $attribute_taxonomies as $tax ) :
$attribute = strtolower ( sanitize_title ( $tax -> attribute_name ));
2011-09-11 13:28:15 +00:00
$taxonomy = $woocommerce -> attribute_taxonomy_name ( $attribute );
2011-09-04 00:02:44 +00:00
$name = 'filter_' . $attribute ;
if ( isset ( $_GET [ $name ]) && taxonomy_exists ( $taxonomy )) $_chosen_attributes [ $taxonomy ] = explode ( ',' , $_GET [ $name ] );
endforeach ;
endif ;
}
/**
* Layered Nav post filter
*/
add_filter ( 'loop-shop-posts-in' , 'woocommerce_layered_nav_query' );
function woocommerce_layered_nav_query ( $filtered_posts ) {
2011-09-06 11:11:22 +00:00
global $_chosen_attributes , $wpdb , $woocommerce ;
2011-09-04 00:02:44 +00:00
if ( sizeof ( $_chosen_attributes ) > 0 ) :
$matched_products = array ();
$filtered = false ;
foreach ( $_chosen_attributes as $attribute => $values ) :
if ( sizeof ( $values ) > 0 ) :
foreach ( $values as $value ) :
$posts = get_objects_in_term ( $value , $attribute );
if ( ! is_wp_error ( $posts ) && ( sizeof ( $matched_products ) > 0 || $filtered )) :
$matched_products = array_intersect ( $posts , $matched_products );
elseif ( ! is_wp_error ( $posts )) :
$matched_products = $posts ;
endif ;
$filtered = true ;
endforeach ;
endif ;
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 ;
endif ;
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' ;
$this -> woo_widget_description = __ ( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.' , 'woothemes' );
$this -> woo_widget_idbase = 'woocommerce_layered_nav' ;
$this -> woo_widget_name = __ ( 'WooCommerce Layered Nav' , 'woothemes' );
/* 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 );
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 );
if ( ! is_tax ( 'product_cat' ) && ! is_post_type_archive ( 'product' ) && ! is_tax ( 'product_tag' )) return ;
2011-09-06 11:11:22 +00:00
global $_chosen_attributes , $wpdb , $woocommerce ;
2011-08-10 17:11:11 +00:00
$title = $instance [ 'title' ];
2011-09-11 13:28:15 +00:00
$taxonomy = $woocommerce -> attribute_taxonomy_name ( $instance [ 'attribute' ]);
2011-08-10 17:11:11 +00:00
if ( ! taxonomy_exists ( $taxonomy )) return ;
$title = apply_filters ( 'widget_title' , $title , $instance , $this -> id_base );
$args = array (
'hide_empty' => '1'
);
$terms = get_terms ( $taxonomy , $args );
$count = count ( $terms );
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 ;
echo " <ul> " ;
2011-08-31 00:29:02 +00:00
2011-08-31 10:51:50 +00:00
// Force found when option is selected
if ( array_key_exists ( $taxonomy , $_chosen_attributes )) $found = true ;
2011-08-10 17:11:11 +00:00
foreach ( $terms as $term ) {
$_products_in_term = get_objects_in_term ( $term -> term_id , $taxonomy );
2011-09-16 08:10:12 +00:00
$count = sizeof ( array_intersect ( $_products_in_term , $woocommerce -> query -> filtered_product_ids ));
2011-08-31 10:51:50 +00:00
2011-08-10 17:11:11 +00:00
if ( $count > 0 ) $found = true ;
2011-08-31 10:51:50 +00:00
$option_is_set = ( isset ( $_chosen_attributes [ $taxonomy ]) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ]));
if ( $count == 0 && ! $option_is_set ) continue ;
2011-08-10 17:11:11 +00:00
$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 = '' ;
elseif ( is_post_type_archive ( 'product' ) || is_page ( get_option ( 'woocommerce_shop_page_id' ) )) :
$link = get_post_type_archive_link ( 'product' );
else :
$link = get_term_link ( get_query_var ( 'term' ), get_query_var ( 'taxonomy' ) );
endif ;
// All current filters
if ( $_chosen_attributes ) foreach ( $_chosen_attributes as $name => $value ) :
if ( $name !== $taxonomy ) :
2011-08-31 12:31:47 +00:00
$link = add_query_arg ( strtolower ( sanitize_title ( str_replace ( 'pa_' , 'filter_' , $name ))), implode ( ',' , $value ), $link );
2011-08-10 17:11:11 +00:00
endif ;
endforeach ;
// 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 );
endif ;
// Current Filter = this widget
if ( isset ( $_chosen_attributes [ $taxonomy ] ) && is_array ( $_chosen_attributes [ $taxonomy ]) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ])) :
$class = 'class="chosen"' ;
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 ;
echo '<li ' . $class . '>' ;
2011-08-31 10:51:50 +00:00
if ( $count > 0 || $option_is_set ) echo '<a href="' . $link . '">' ; else echo '<span>' ;
2011-08-10 17:11:11 +00:00
echo $term -> name ;
2011-08-31 10:51:50 +00:00
if ( $count > 0 || $option_is_set ) echo '</a>' ; else echo '</span>' ;
2011-08-10 17:11:11 +00:00
echo ' <small class="count">' . $count . '</small></li>' ;
}
echo " </ul> " ;
echo $after_widget ;
if ( ! $found ) :
ob_clean ();
return ;
else :
$widget = ob_get_clean ();
echo $widget ;
endif ;
}
}
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' ]);
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-09-06 11:11:22 +00:00
global $wpdb , $woocommerce ;
2011-08-10 17:11:11 +00:00
?>
< p >< label for = " <?php echo $this->get_field_id ('title'); ?> " >< ? php _e ( 'Title:' , 'woothemes' ) ?> </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
< p >< label for = " <?php echo $this->get_field_id ('attribute'); ?> " >< ? php _e ( 'Attribute:' , 'woothemes' ) ?> </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 ;
?>
</ select >
< ? php
}
} // class WooCommerce_Widget_Layered_Nav