2012-05-26 16:25:07 +00:00
< ? php
/**
2012-08-14 17:37:50 +00:00
* Layered Navigation Widget
*
* @ author WooThemes
* @ category Widgets
* @ package WooCommerce / Widgets
* @ version 1.6 . 4
* @ extends WP_Widget
2012-05-26 16:25:07 +00:00
*/
class WooCommerce_Widget_Layered_Nav extends WP_Widget {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
var $woo_widget_cssclass ;
var $woo_widget_description ;
var $woo_widget_idbase ;
var $woo_widget_name ;
2012-08-14 17:37:50 +00:00
/**
* constructor
*
* @ access public
* @ return void
*/
2012-05-26 16:25:07 +00:00
function WooCommerce_Widget_Layered_Nav () {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
/* Widget variable settings. */
2012-08-28 15:18:16 +00:00
$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' );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
/* Widget settings. */
$widget_ops = array ( 'classname' => $this -> woo_widget_cssclass , 'description' => $this -> woo_widget_description );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
/* Create the widget. */
2012-08-28 15:18:16 +00:00
$this -> WP_Widget ( 'woocommerce_layered_nav' , $this -> woo_widget_name , $widget_ops );
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
/**
* widget function .
*
* @ see WP_Widget
* @ access public
* @ param array $args
* @ param array $instance
* @ return void
*/
2012-05-26 16:25:07 +00:00
function widget ( $args , $instance ) {
global $_chosen_attributes , $woocommerce , $_attributes_array ;
2012-08-28 15:18:16 +00:00
extract ( $args );
2012-05-26 16:25:07 +00:00
2012-08-28 15:18:16 +00:00
if ( ! is_post_type_archive ( 'product' ) && ! is_tax ( array_merge ( $_attributes_array , array ( 'product_cat' , 'product_tag' ) ) ) )
return ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$current_term = $_attributes_array && is_tax ( $_attributes_array ) ? get_queried_object () -> term_id : '' ;
$current_tax = $_attributes_array && is_tax ( $_attributes_array ) ? get_queried_object () -> taxonomy : '' ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +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' ;
$display_type = isset ( $instance [ 'display_type' ] ) ? $instance [ 'display_type' ] : 'list' ;
2012-05-26 16:25:07 +00:00
2012-08-28 15:18:16 +00:00
if ( ! taxonomy_exists ( $taxonomy ) )
return ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$terms = get_terms ( $taxonomy , array ( 'hide_empty' => '1' ) );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( count ( $terms ) > 0 ) {
2012-05-26 16:25:07 +00:00
ob_start ();
2012-08-28 15:18:16 +00:00
$found = false ;
2012-05-26 16:25:07 +00:00
echo $before_widget . $before_title . $title . $after_title ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Force found when option is selected - do not force found on taxonomy attributes
2012-08-28 15:18:16 +00:00
if ( ! $_attributes_array || ! is_tax ( $_attributes_array ) )
if ( is_array ( $_chosen_attributes ) && array_key_exists ( $taxonomy , $_chosen_attributes ) )
$found = true ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $display_type == 'dropdown' ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// skip when viewing the taxonomy
if ( $current_tax && $taxonomy == $current_tax ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$found = false ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
} else {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$taxonomy_filter = str_replace ( 'pa_' , '' , $taxonomy );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$found = true ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
echo '<select id="dropdown_layered_nav_' . $taxonomy_filter . '">' ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
echo '<option value="">' . sprintf ( __ ( 'Any %s' , 'woocommerce' ), $woocommerce -> attribute_label ( $taxonomy ) ) . '</option>' ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
foreach ( $terms as $term ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// If on a term page, skip that term in widget list
2012-08-28 15:18:16 +00:00
if ( $term -> term_id == $current_term )
continue ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Get count based on current view - uses transients
2012-08-28 15:18:16 +00:00
$transient_name = 'wc_ln_count_' . md5 ( sanitize_key ( $taxonomy ) . sanitize_key ( $term -> term_id ) );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
if ( false === ( $_products_in_term = get_transient ( $transient_name ) ) ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$_products_in_term = get_objects_in_term ( $term -> term_id , $taxonomy );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
set_transient ( $transient_name , $_products_in_term );
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$option_is_set = ( isset ( $_chosen_attributes [ $taxonomy ] ) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ][ 'terms' ] ) );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// If this is an AND query, only show options with count > 0
2012-08-28 15:18:16 +00:00
if ( $query_type == 'and' ) {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$count = sizeof ( array_intersect ( $_products_in_term , $woocommerce -> query -> filtered_product_ids ) );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $count > 0 )
$found = true ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $count == 0 && ! $option_is_set )
continue ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// If this is an OR query, show all options so search can be expanded
} else {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$count = sizeof ( array_intersect ( $_products_in_term , $woocommerce -> query -> unfiltered_product_ids ) );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $count > 0 )
$found = true ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +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>' ;
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
echo '</select>' ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$woocommerce -> add_inline_js ( "
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
jQuery ( '#dropdown_layered_nav_$taxonomy_filter' ) . change ( function (){
2012-08-14 17:37:50 +00:00
2012-10-09 12:13:07 +00:00
location . href = '" . preg_replace( ' % \ / page / [ 0 - 9 ] +% ', ' ', add_query_arg(' filtering ', ' 1 ', remove_query_arg( array( ' page ', ' filter_ ' . $taxonomy_filter ) ) ) ) . "&filter_$taxonomy_filter=' + jQuery ( '#dropdown_layered_nav_$taxonomy_filter' ) . val ();
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
});
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
" );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
} else {
// List display
echo " <ul> " ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
foreach ( $terms as $term ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Get count based on current view - uses transients
2012-08-28 15:18:16 +00:00
$transient_name = 'wc_ln_count_' . md5 ( sanitize_key ( $taxonomy ) . sanitize_key ( $term -> term_id ) );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
if ( false === ( $_products_in_term = get_transient ( $transient_name ) ) ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$_products_in_term = get_objects_in_term ( $term -> term_id , $taxonomy );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
set_transient ( $transient_name , $_products_in_term );
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$option_is_set = ( isset ( $_chosen_attributes [ $taxonomy ] ) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ][ 'terms' ] ) );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// If this is an AND query, only show options with count > 0
2012-08-28 15:18:16 +00:00
if ( $query_type == 'and' ) {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$count = sizeof ( array_intersect ( $_products_in_term , $woocommerce -> query -> filtered_product_ids ) );
2012-05-26 16:25:07 +00:00
// skip the term for the current archive
2012-08-28 15:18:16 +00:00
if ( $current_term == $term -> term_id )
continue ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $count > 0 && $current_term !== $term -> term_id )
$found = true ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $count == 0 && ! $option_is_set )
continue ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// If this is an OR query, show all options so search can be expanded
2012-08-14 17:37:50 +00:00
} else {
2012-05-26 16:25:07 +00:00
// skip the term for the current archive
2012-08-28 15:18:16 +00:00
if ( $current_term == $term -> term_id )
continue ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$count = sizeof ( array_intersect ( $_products_in_term , $woocommerce -> query -> unfiltered_product_ids ) );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $count > 0 )
$found = true ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$arg = 'filter_' . sanitize_title ( $instance [ 'attribute' ] );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$current_filter = ( isset ( $_GET [ $arg ] ) ) ? explode ( ',' , $_GET [ $arg ] ) : array ();
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! is_array ( $current_filter ) )
$current_filter = array ();
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! in_array ( $term -> term_id , $current_filter ) )
$current_filter [] = $term -> term_id ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Base Link decided by current page
2012-08-28 15:18:16 +00:00
if ( defined ( 'SHOP_IS_ON_FRONT' ) ) {
2012-05-26 16:25:07 +00:00
$link = home_url ();
2012-08-28 15:18:16 +00:00
} elseif ( is_post_type_archive ( 'product' ) || is_page ( woocommerce_get_page_id ( 'shop' ) ) ) {
$link = get_post_type_archive_link ( 'product' );
} else {
2012-05-26 16:25:07 +00:00
$link = get_term_link ( get_query_var ( 'term' ), get_query_var ( 'taxonomy' ) );
2012-08-28 15:18:16 +00:00
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// All current filters
2012-08-28 15:18:16 +00:00
if ( $_chosen_attributes ) {
foreach ( $_chosen_attributes as $name => $data ) {
if ( $name !== $taxonomy ) {
2012-08-14 17:37:50 +00:00
//exclude query arg for current term archive term
2012-08-28 15:18:16 +00:00
while ( in_array ( $current_term , $data [ 'terms' ] ) ) {
$key = array_search ( $current_term , $data );
unset ( $data [ 'terms' ][ $key ] );
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! empty ( $data [ 'terms' ] ) )
$link = add_query_arg ( sanitize_title ( str_replace ( 'pa_' , 'filter_' , $name ) ), implode ( ',' , $data [ 'terms' ]), $link );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $data [ 'query_type' ] == 'or' )
$link = add_query_arg ( sanitize_title ( str_replace ( 'pa_' , 'query_type_' , $name ) ), 'or' , $link );
}
}
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Min/Max
2012-08-28 15:18:16 +00:00
if ( isset ( $_GET [ 'min_price' ] ) )
2012-05-26 16:25:07 +00:00
$link = add_query_arg ( 'min_price' , $_GET [ 'min_price' ], $link );
2012-08-28 15:18:16 +00:00
if ( isset ( $_GET [ 'max_price' ] ) )
2012-05-26 16:25:07 +00:00
$link = add_query_arg ( 'max_price' , $_GET [ 'max_price' ], $link );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Current Filter = this widget
2012-08-28 15:18:16 +00:00
if ( isset ( $_chosen_attributes [ $taxonomy ] ) && is_array ( $_chosen_attributes [ $taxonomy ][ 'terms' ] ) && in_array ( $term -> term_id , $_chosen_attributes [ $taxonomy ][ 'terms' ] ) ) {
2012-05-26 16:25:07 +00:00
$class = 'class="chosen"' ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Remove this term is $current_filter has more than 1 term filtered
2012-08-28 15:18:16 +00:00
if ( sizeof ( $current_filter ) > 1 ) {
$current_filter_without_this = array_diff ( $current_filter , array ( $term -> term_id ) );
$link = add_query_arg ( $arg , implode ( ',' , $current_filter_without_this ), $link );
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
} else {
$class = '' ;
$link = add_query_arg ( $arg , implode ( ',' , $current_filter ), $link );
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Search Arg
2012-08-28 15:18:16 +00:00
if ( get_search_query () )
2012-05-26 16:25:07 +00:00
$link = add_query_arg ( 's' , get_search_query (), $link );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Post Type Arg
2012-08-28 15:18:16 +00:00
if ( isset ( $_GET [ 'post_type' ] ) )
2012-05-26 16:25:07 +00:00
$link = add_query_arg ( 'post_type' , $_GET [ 'post_type' ], $link );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
// Query type Arg
2012-08-28 15:18:16 +00:00
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_' . sanitize_title ( $instance [ 'attribute' ] ), 'or' , $link );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
echo '<li ' . $class . '>' ;
2012-08-14 17:37:50 +00:00
2012-08-30 12:24:55 +00:00
echo ( $count > 0 || $option_is_set ) ? '<a href="' . apply_filters ( 'woocommerce_layered_nav_link' , $link ) . '">' : '<span>' ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
echo $term -> name ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
echo ( $count > 0 || $option_is_set ) ? '</a>' : '</span>' ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
echo ' <small class="count">' . $count . '</small></li>' ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
echo " </ul> " ;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
} // End display type conditional
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
echo $after_widget ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! $found )
2012-08-14 17:37:50 +00:00
ob_clean ();
2012-08-28 15:18:16 +00:00
else
echo ob_get_clean ();
2012-05-26 16:25:07 +00:00
}
}
2012-08-14 17:37:50 +00:00
/**
* update function .
*
* @ see WP_Widget -> update
* @ access public
* @ param array $new_instance
* @ param array $old_instance
* @ return array
*/
2012-05-26 16:25:07 +00:00
function update ( $new_instance , $old_instance ) {
global $woocommerce ;
2012-08-28 15:18:16 +00:00
if ( 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' ] );
2012-05-26 16:25:07 +00:00
return $instance ;
}
2012-08-14 17:37:50 +00:00
/**
* form function .
*
* @ see WP_Widget -> form
* @ access public
* @ param array $instance
* @ return void
*/
2012-05-26 16:25:07 +00:00
function form ( $instance ) {
global $woocommerce ;
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! isset ( $instance [ 'query_type' ] ) )
$instance [ 'query_type' ] = 'and' ;
if ( ! isset ( $instance [ 'display_type' ] ) )
$instance [ 'display_type' ] = 'list' ;
2012-05-26 16:25:07 +00:00
?>
2012-08-28 15:18:16 +00:00
< p >< label for = " <?php echo $this->get_field_id ( 'title' ); ?> " >< ? php _e ( 'Title:' , 'woocommerce' ) ?> </label>
< 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 >
< p >< label for = " <?php echo $this->get_field_id ( 'attribute' ); ?> " >< ? php _e ( 'Attribute:' , 'woocommerce' ) ?> </label>
< select id = " <?php echo esc_attr( $this->get_field_id ( 'attribute' ) ); ?> " name = " <?php echo esc_attr( $this->get_field_name ( 'attribute' ) ); ?> " >
< ? php
$attribute_taxonomies = $woocommerce -> get_attribute_taxonomies ();
if ( $attribute_taxonomies )
foreach ( $attribute_taxonomies as $tax )
if ( taxonomy_exists ( $woocommerce -> attribute_taxonomy_name ( $tax -> attribute_name ) ) )
echo '<option value="' . $tax -> attribute_name . '" ' . selected ( ( isset ( $instance [ 'attribute' ] ) && $instance [ 'attribute' ] == $tax -> attribute_name ), true , false ) . '>' . $tax -> attribute_name . '</option>' ;
?>
</ select ></ p >
< 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 >
< p >< label for = " <?php echo $this->get_field_id ( 'query_type' ); ?> " >< ? php _e ( 'Query Type:' , 'woocommerce' ) ?> </label>
< select id = " <?php echo esc_attr( $this->get_field_id ( 'query_type' ) ); ?> " name = " <?php echo esc_attr( $this->get_field_name ( 'query_type' ) ); ?> " >
< 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>
</ select ></ p >
2012-05-26 16:25:07 +00:00
< ? php
}
2012-08-14 17:37:50 +00:00
}
/**
* Layered Nav Init
*
2012-08-15 18:15:06 +00:00
* @ package WooCommerce / Widgets
2012-08-14 17:37:50 +00:00
* @ access public
* @ return void
*/
function woocommerce_layered_nav_init ( ) {
if ( is_active_widget ( false , false , 'woocommerce_layered_nav' , true ) && ! is_admin () ) {
global $_chosen_attributes , $woocommerce , $_attributes_array ;
2012-08-28 15:18:16 +00:00
$_chosen_attributes = $_attributes_array = array ();
2012-08-14 17:37:50 +00:00
$attribute_taxonomies = $woocommerce -> attribute_taxonomies ;
if ( $attribute_taxonomies ) {
foreach ( $attribute_taxonomies as $tax ) {
2012-08-28 15:18:16 +00:00
$attribute = sanitize_title ( $tax -> attribute_name );
$taxonomy = $woocommerce -> attribute_taxonomy_name ( $attribute );
2012-08-14 17:37:50 +00:00
// create an array of product attribute taxonomies
$_attributes_array [] = $taxonomy ;
$name = 'filter_' . $attribute ;
$query_type_name = 'query_type_' . $attribute ;
2012-08-28 15:18:16 +00:00
if ( ! empty ( $_GET [ $name ] ) && taxonomy_exists ( $taxonomy ) ) {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
$_chosen_attributes [ $taxonomy ][ 'terms' ] = explode ( ',' , $_GET [ $name ] );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! empty ( $_GET [ $query_type_name ] ) && $_GET [ $query_type_name ] == 'or' )
$_chosen_attributes [ $taxonomy ][ 'query_type' ] = 'or' ;
2012-08-14 17:37:50 +00:00
else
2012-08-28 15:18:16 +00:00
$_chosen_attributes [ $taxonomy ][ 'query_type' ] = 'and' ;
2012-08-14 17:37:50 +00:00
}
}
}
add_filter ( 'loop_shop_post_in' , 'woocommerce_layered_nav_query' );
}
}
add_action ( 'init' , 'woocommerce_layered_nav_init' , 1 );
/**
* Layered Nav post filter
*
2012-08-15 18:15:06 +00:00
* @ package WooCommerce / Widgets
2012-08-14 17:37:50 +00:00
* @ access public
* @ param array $filtered_posts
* @ return array
*/
function woocommerce_layered_nav_query ( $filtered_posts ) {
global $_chosen_attributes , $woocommerce , $wp_query ;
2012-08-28 15:18:16 +00:00
if ( sizeof ( $_chosen_attributes ) > 0 ) {
2012-08-14 17:37:50 +00:00
$matched_products = array ();
$filtered_attribute = false ;
2012-08-28 15:18:16 +00:00
foreach ( $_chosen_attributes as $attribute => $data ) {
2012-08-14 17:37:50 +00:00
$matched_products_from_attribute = array ();
$filtered = false ;
2012-08-28 15:18:16 +00:00
if ( sizeof ( $data [ 'terms' ] ) > 0 ) {
foreach ( $data [ 'terms' ] as $value ) {
2012-08-14 17:37:50 +00:00
$posts = get_posts (
array (
'post_type' => 'product' ,
'numberposts' => - 1 ,
'post_status' => 'publish' ,
'fields' => 'ids' ,
'no_found_rows' => true ,
'tax_query' => array (
array (
'taxonomy' => $attribute ,
'terms' => $value ,
'field' => 'id'
)
)
)
);
// AND or OR
2012-08-28 15:18:16 +00:00
if ( $data [ 'query_type' ] == 'or' ) {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! is_wp_error ( $posts ) && ( sizeof ( $matched_products_from_attribute ) > 0 || $filtered ) )
2012-08-14 17:37:50 +00:00
$matched_products_from_attribute = array_merge ( $posts , $matched_products_from_attribute );
2012-08-28 15:18:16 +00:00
elseif ( ! is_wp_error ( $posts ) )
2012-08-14 17:37:50 +00:00
$matched_products_from_attribute = $posts ;
2012-08-28 15:18:16 +00:00
} else {
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( ! is_wp_error ( $posts ) && ( sizeof ( $matched_products_from_attribute ) > 0 || $filtered ) )
2012-08-14 17:37:50 +00:00
$matched_products_from_attribute = array_intersect ( $posts , $matched_products_from_attribute );
2012-08-28 15:18:16 +00:00
elseif ( ! is_wp_error ( $posts ) )
2012-08-14 17:37:50 +00:00
$matched_products_from_attribute = $posts ;
2012-08-28 15:18:16 +00:00
}
2012-08-14 17:37:50 +00:00
$filtered = true ;
2012-08-28 15:18:16 +00:00
}
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( sizeof ( $matched_products ) > 0 || $filtered_attribute )
$matched_products = array_intersect ( $matched_products_from_attribute , $matched_products );
else
2012-08-14 17:37:50 +00:00
$matched_products = $matched_products_from_attribute ;
$filtered_attribute = true ;
2012-08-28 15:18:16 +00:00
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( $filtered ) {
2012-08-14 17:37:50 +00:00
$woocommerce -> query -> layered_nav_post__in = $matched_products ;
$woocommerce -> query -> layered_nav_post__in [] = 0 ;
2012-08-28 15:18:16 +00:00
if ( sizeof ( $filtered_posts ) == 0 ) {
2012-08-14 17:37:50 +00:00
$filtered_posts = $matched_products ;
$filtered_posts [] = 0 ;
2012-08-28 15:18:16 +00:00
} else {
$filtered_posts = array_intersect ( $filtered_posts , $matched_products );
2012-08-14 17:37:50 +00:00
$filtered_posts [] = 0 ;
2012-08-28 15:18:16 +00:00
}
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
}
}
2012-08-14 17:37:50 +00:00
return ( array ) $filtered_posts ;
}