woocommerce/includes/widgets/class-wc-widget-layered-nav...

105 lines
3.1 KiB
PHP
Raw Normal View History

<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
2015-12-17 07:23:58 +00:00
* Layered Navigation Filters Widget.
*
* @author WooThemes
* @category Widgets
* @package WooCommerce/Widgets
* @version 2.3.0
* @extends WC_Widget
*/
class WC_Widget_Layered_Nav_Filters extends WC_Widget {
/**
2015-11-03 13:31:20 +00:00
* Constructor.
*/
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' )
)
);
parent::__construct();
}
/**
* Output widget.
*
* @see WP_Widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
global $_chosen_attributes;
2012-11-27 16:22:47 +00:00
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
return;
}
2012-11-27 16:22:47 +00:00
// Price
2013-01-12 13:03:19 +00:00
$min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : 0;
$max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : 0;
2012-11-27 16:22:47 +00:00
if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price ) {
2012-11-27 16:22:47 +00:00
$this->widget_start( $args, $instance );
echo '<ul>';
2012-11-27 16:22:47 +00:00
// Attributes
if ( ! is_null( $_chosen_attributes ) ) {
2013-08-04 03:59:26 +00:00
foreach ( $_chosen_attributes as $taxonomy => $data ) {
2012-11-27 16:22:47 +00:00
2013-08-04 03:59:26 +00:00
foreach ( $data['terms'] as $term_id ) {
$term = get_term( $term_id, $taxonomy );
if ( ! isset( $term->name ) ) {
continue;
}
$taxonomy_filter = str_replace( 'pa_', '', $taxonomy );
$current_filter = ! empty( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '';
$new_filter = array_map( 'absint', explode( ',', $current_filter ) );
$new_filter = array_diff( $new_filter, array( $term_id ) );
2012-11-27 16:22:47 +00:00
$link = remove_query_arg( array( 'add-to-cart', 'filter_' . $taxonomy_filter ) );
2012-11-27 16:22:47 +00:00
if ( sizeof( $new_filter ) > 0 ) {
2013-08-04 03:59:26 +00:00
$link = add_query_arg( 'filter_' . $taxonomy_filter, implode( ',', $new_filter ), $link );
}
2012-11-27 16:22:47 +00:00
2015-08-05 19:17:52 +00:00
echo '<li class="chosen"><a title="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . $term->name . '</a></li>';
2013-08-04 03:59:26 +00:00
}
}
}
2012-11-27 16:22:47 +00:00
2013-01-12 13:03:19 +00:00
if ( $min_price ) {
$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-11-27 16:22:47 +00:00
2013-01-12 13:03:19 +00:00
if ( $max_price ) {
$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-11-27 16:22:47 +00:00
echo '</ul>';
$this->widget_end( $args );
}
}
}