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
2013-05-24 15:51:58 +00:00
* @ version 2.1 . 0
* @ extends WC_Widget
2012-05-26 16:25:07 +00:00
*/
2012-10-15 10:57:58 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2013-05-24 15:51:58 +00:00
class WC_Widget_Layered_Nav extends WC_Widget {
2012-08-14 17:37:50 +00:00
/**
2013-05-24 15:51:58 +00:00
* Constructor
2012-08-14 17:37:50 +00:00
*/
2013-05-24 15:51:58 +00:00
public function __construct () {
global $woocommerce ;
2012-08-14 17:37:50 +00:00
2013-05-24 15:51:58 +00:00
$this -> widget_cssclass = 'woocommerce widget_layered_nav' ;
$this -> widget_description = __ ( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.' , 'woocommerce' );
$this -> widget_id = 'woocommerce_layered_nav' ;
$this -> widget_name = __ ( 'WooCommerce Layered Nav' , 'woocommerce' );
2012-08-14 17:37:50 +00:00
2013-06-20 11:05:07 +00:00
parent :: __construct ();
}
/**
* update function .
*
* @ see WP_Widget -> update
* @ access public
* @ param array $new_instance
* @ param array $old_instance
* @ return array
*/
public function update ( $new_instance , $old_instance ) {
$this -> init_settings ();
return parent :: update ( $new_instance , $old_instance );
}
/**
* form function .
*
* @ see WP_Widget -> form
* @ access public
* @ param array $instance
* @ return void
*/
public function form ( $instance ) {
$this -> init_settings ();
return parent :: form ( $instance );
}
/**
* Init settings after post types are registered
*/
public function init_settings () {
global $woocommerce ;
2013-05-24 15:51:58 +00:00
$attribute_array = array ();
2013-06-05 14:14:25 +00:00
$attribute_taxonomies = $woocommerce -> get_helper ( 'attribute' ) -> get_attribute_taxonomies ();
2013-05-24 15:51:58 +00:00
if ( $attribute_taxonomies )
foreach ( $attribute_taxonomies as $tax )
2013-06-05 14:14:25 +00:00
if ( taxonomy_exists ( $woocommerce -> get_helper ( 'attribute' ) -> attribute_taxonomy_name ( $tax -> attribute_name ) ) )
2013-05-24 15:51:58 +00:00
$attribute_array [ $tax -> attribute_name ] = $tax -> attribute_name ;
$this -> settings = array (
'title' => array (
'type' => 'text' ,
'std' => __ ( 'Filter by' , 'woocommerce' ),
'label' => __ ( 'Title' , 'woocommerce' )
),
'attribute' => array (
'type' => 'select' ,
'std' => '' ,
'label' => __ ( 'Attribute' , 'woocommerce' ),
'options' => $attribute_array
),
'display_type' => array (
'type' => 'select' ,
'std' => 'list' ,
'label' => __ ( 'Display type' , 'woocommerce' ),
'options' => array (
'list' => __ ( 'List' , 'woocommerce' ),
'dropdown' => __ ( 'Dropdown' , 'woocommerce' )
)
),
'query_type' => array (
'type' => 'select' ,
'std' => 'and' ,
'label' => __ ( 'Query type' , 'woocommerce' ),
'options' => array (
'and' => __ ( 'AND' , 'woocommerce' ),
'or' => __ ( 'OR' , 'woocommerce' )
)
),
);
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
*/
2013-05-24 15:51:58 +00:00
public function widget ( $args , $instance ) {
2013-06-20 10:21:31 +00:00
global $_chosen_attributes , $woocommerce ;
2012-11-27 16:22:47 +00:00
2012-08-28 15:18:16 +00:00
extract ( $args );
2012-05-26 16:25:07 +00:00
2013-06-20 10:21:31 +00:00
if ( ! is_post_type_archive ( 'product' ) && ! is_tax ( get_object_taxonomies ( 'product' ) ) )
2012-08-28 15:18:16 +00:00
return ;
2012-08-14 17:37:50 +00:00
2013-06-20 10:21:31 +00:00
$current_term = is_tax () ? get_queried_object () -> term_id : '' ;
$current_tax = is_tax () ? get_queried_object () -> taxonomy : '' ;
2012-08-28 15:18:16 +00:00
$title = apply_filters ( 'widget_title' , $instance [ 'title' ], $instance , $this -> id_base );
2013-06-05 14:14:25 +00:00
$taxonomy = $woocommerce -> get_helper ( 'attribute' ) -> attribute_taxonomy_name ( $instance [ 'attribute' ]);
2012-08-28 15:18:16 +00:00
$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-11-27 16:22:47 +00:00
if ( ! taxonomy_exists ( $taxonomy ) )
2012-08-28 15:18:16 +00:00
return ;
2012-08-14 17:37:50 +00:00
2013-08-19 13:31:48 +00:00
$get_terms_args = array ( 'hide_empty' => '1' );
$orderby = WC () -> get_helper ( 'attribute' ) -> attribute_orderby ( $taxonomy );
switch ( $orderby ) {
case 'name' :
$get_terms_args [ 'orderby' ] = 'name' ;
$get_terms_args [ 'menu_order' ] = false ;
break ;
case 'id' :
$get_terms_args [ 'orderby' ] = 'id' ;
$get_terms_args [ 'order' ] = 'ASC' ;
$get_terms_args [ 'menu_order' ] = false ;
break ;
case 'menu_order' :
$get_terms_args [ 'menu_order' ] = 'ASC' ;
break ;
}
$terms = get_terms ( $taxonomy , $get_terms_args );
2012-08-14 17:37:50 +00:00
2012-08-28 15:18:16 +00:00
if ( count ( $terms ) > 0 ) {
2012-11-27 16:22:47 +00:00
2012-05-26 16:25:07 +00:00
ob_start ();
2012-11-27 16:22:47 +00:00
2012-08-28 15:18:16 +00:00
$found = false ;
2012-11-27 16:22:47 +00:00
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
2013-06-20 10:21:31 +00:00
if ( ! is_tax () && 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-12-19 18:57:11 +00:00
$found = false ;
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
2013-06-05 14:14:25 +00:00
echo '<option value="">' . sprintf ( __ ( 'Any %s' , 'woocommerce' ), $woocommerce -> get_helper ( 'attribute' ) -> 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-11-27 16:22:47 +00:00
if ( $term -> term_id == $current_term )
2012-08-28 15:18:16 +00:00
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-11-27 16:22:47 +00:00
if ( $count > 0 )
2012-08-28 15:18:16 +00:00
$found = true ;
2012-08-14 17:37:50 +00:00
2012-11-27 16:22:47 +00:00
if ( $count == 0 && ! $option_is_set )
2012-08-28 15:18:16 +00:00
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-11-27 16:22:47 +00:00
if ( $count > 0 )
2012-08-28 15:18:16 +00:00
$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
2013-06-05 11:51:06 +00:00
$woocommerce -> get_helper ( 'inline-javascript' ) -> 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
2013-08-19 12:12:49 +00:00
location . href = '" . esc_url_raw( 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-11-27 16:22:47 +00:00
if ( $current_term == $term -> term_id )
2012-08-28 15:18:16 +00:00
continue ;
2012-08-14 17:37:50 +00:00
2012-11-27 16:22:47 +00:00
if ( $count > 0 && $current_term !== $term -> term_id )
2012-08-28 15:18:16 +00:00
$found = true ;
2012-08-14 17:37:50 +00:00
2012-11-27 16:22:47 +00:00
if ( $count == 0 && ! $option_is_set )
2012-08-28 15:18:16 +00:00
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-11-27 16:22:47 +00:00
if ( $current_term == $term -> term_id )
2012-08-28 15:18:16 +00:00
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-11-27 16:22:47 +00:00
if ( $count > 0 )
2012-08-28 15:18:16 +00:00
$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-11-27 16:22:47 +00:00
if ( ! is_array ( $current_filter ) )
2012-08-28 15:18:16 +00:00
$current_filter = array ();
2012-08-14 17:37:50 +00:00
2012-10-16 13:06:06 +00:00
$current_filter = array_map ( 'esc_attr' , $current_filter );
2012-11-27 16:22:47 +00:00
if ( ! in_array ( $term -> term_id , $current_filter ) )
2012-08-28 15:18:16 +00:00
$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
2013-05-08 11:16:40 +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
2013-05-08 11:16:40 +00:00
// Remove pa_ and sanitize
$filter_name = sanitize_title ( str_replace ( 'pa_' , '' , $name ) );
2012-08-28 15:18:16 +00:00
if ( ! empty ( $data [ 'terms' ] ) )
2013-05-08 11:16:40 +00:00
$link = add_query_arg ( 'filter_' . $filter_name , implode ( ',' , $data [ 'terms' ] ), $link );
2012-08-14 17:37:50 +00:00
2012-11-27 16:22:47 +00:00
if ( $data [ 'query_type' ] == 'or' )
2013-05-08 11:16:40 +00:00
$link = add_query_arg ( 'query_type_' . $filter_name , 'or' , $link );
2012-08-28 15:18:16 +00:00
}
}
}
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-11-27 16:22:47 +00:00
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 {
2012-11-27 16:22:47 +00:00
2012-08-28 15:18:16 +00:00
$class = '' ;
$link = add_query_arg ( $arg , implode ( ',' , $current_filter ), $link );
2012-11-27 16:22:47 +00:00
2012-08-28 15:18:16 +00:00
}
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
2013-03-15 21:28:01 +00:00
echo ( $count > 0 || $option_is_set ) ? '<a href="' . esc_url ( 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-12-29 13:51:51 +00:00
ob_end_clean ();
2012-08-28 15:18:16 +00:00
else
echo ob_get_clean ();
2012-05-26 16:25:07 +00:00
}
}
2013-05-24 15:51:58 +00:00
}
2012-08-14 17:37:50 +00:00
2013-05-24 15:51:58 +00:00
register_widget ( 'WC_Widget_Layered_Nav' );