2012-10-14 12:06:37 +00:00
|
|
|
<?php
|
2014-11-19 17:12:56 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2012-10-14 12:06:37 +00:00
|
|
|
/**
|
2015-12-17 07:23:58 +00:00
|
|
|
* Layered Navigation Filters Widget.
|
2012-10-14 12:06:37 +00:00
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
|
|
|
* @version 2.3.0
|
|
|
|
* @extends WC_Widget
|
2012-10-14 12:06:37 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
class WC_Widget_Layered_Nav_Filters extends WC_Widget {
|
2012-10-14 12:06:37 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2012-10-14 12:06:37 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function __construct() {
|
|
|
|
$this->widget_cssclass = 'woocommerce widget_layered_nav_filters';
|
|
|
|
$this->widget_description = __( 'Shows active layered nav filters so users can see and deactivate them.', 'woocommerce' );
|
|
|
|
$this->widget_id = 'woocommerce_layered_nav_filters';
|
|
|
|
$this->widget_name = __( 'WooCommerce Layered Nav Filters', 'woocommerce' );
|
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'std' => __( 'Active Filters', 'woocommerce' ),
|
|
|
|
'label' => __( 'Title', 'woocommerce' )
|
|
|
|
)
|
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
parent::__construct();
|
2012-10-14 12:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Output widget.
|
2012-10-14 12:06:37 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2012-10-14 12:06:37 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function widget( $args, $instance ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
|
2012-10-14 12:06:37 +00:00
|
|
|
return;
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-02-10 10:19:32 +00:00
|
|
|
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
|
|
|
|
$min_price = isset( $_GET['min_price'] ) ? wc_clean( $_GET['min_price'] ) : 0;
|
|
|
|
$max_price = isset( $_GET['max_price'] ) ? wc_clean( $_GET['max_price'] ) : 0;
|
2016-02-10 11:43:35 +00:00
|
|
|
$min_rating = isset( $_GET['min_rating'] ) ? absint( $_GET['min_rating'] ) : 0;
|
2015-11-13 09:47:27 +00:00
|
|
|
|
|
|
|
if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price || 0 < $min_rating ) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_start( $args, $instance );
|
2012-10-14 12:06:37 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
echo '<ul>';
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-10-14 12:06:37 +00:00
|
|
|
// Attributes
|
2016-02-10 10:19:32 +00:00
|
|
|
if ( $_chosen_attributes ) {
|
2013-08-04 03:59:26 +00:00
|
|
|
foreach ( $_chosen_attributes as $taxonomy => $data ) {
|
2015-12-04 13:55:31 +00:00
|
|
|
foreach ( $data['terms'] as $term_slug ) {
|
2016-02-09 14:41:17 +00:00
|
|
|
if ( ! $term = get_term_by( 'slug', $term_slug, $taxonomy ) ) {
|
2015-06-08 16:10:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
$filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) );
|
|
|
|
$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( $_GET[ $filter_name ] ) ) : array();
|
|
|
|
$current_filter = array_map( 'sanitize_title', $current_filter );
|
|
|
|
$new_filter = array_diff( $current_filter, array( $term_slug ) );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
$link = remove_query_arg( array( 'add-to-cart', $filter_name ) );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( sizeof( $new_filter ) > 0 ) {
|
2016-02-09 14:41:17 +00:00
|
|
|
$link = add_query_arg( $filter_name, implode( ',', $new_filter ), $link );
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-02-09 14:41:17 +00:00
|
|
|
echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a></li>';
|
2013-08-04 03:59:26 +00:00
|
|
|
}
|
2012-10-14 12:06:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-12 13:03:19 +00:00
|
|
|
if ( $min_price ) {
|
2012-10-14 12:06:37 +00:00
|
|
|
$link = remove_query_arg( 'min_price' );
|
2015-08-05 19:17:52 +00:00
|
|
|
echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . __( 'Min', 'woocommerce' ) . ' ' . wc_price( $min_price ) . '</a></li>';
|
2012-10-14 12:06:37 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-01-12 13:03:19 +00:00
|
|
|
if ( $max_price ) {
|
2012-10-14 12:06:37 +00:00
|
|
|
$link = remove_query_arg( 'max_price' );
|
2015-08-05 19:17:52 +00:00
|
|
|
echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . __( 'Max', 'woocommerce' ) . ' ' . wc_price( $max_price ) . '</a></li>';
|
2012-10-14 12:06:37 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-02-09 15:18:27 +00:00
|
|
|
if ( $min_rating ) {
|
2015-11-13 09:47:27 +00:00
|
|
|
$link = remove_query_arg( 'min_rating' );
|
|
|
|
echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Rated %s and above', 'woocommerce' ), $min_rating ) . '</a></li>';
|
|
|
|
}
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
echo '</ul>';
|
2012-10-14 12:06:37 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_end( $args );
|
2012-10-14 12:06:37 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|