1 <?php
  2 /**
  3  * Layered Navigation Fitlers Widget
  4  *
  5  * @author      WooThemes
  6  * @category    Widgets
  7  * @package     WooCommerce/Widgets
  8  * @version     2.0.1
  9  * @extends     WC_Widget
 10  */
 11 
 12 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 13 
 14 class WC_Widget_Layered_Nav_Filters extends WC_Widget {
 15 
 16     /**
 17      * Constructor
 18      */
 19     public function __construct() {
 20         $this->widget_cssclass    = 'woocommerce widget_layered_nav_filters';
 21         $this->widget_description = __( 'Shows active layered nav filters so users can see and deactivate them.', 'woocommerce' );
 22         $this->widget_id          = 'woocommerce_layered_nav_filters';
 23         $this->widget_name        = __( 'WooCommerce Layered Nav Filters', 'woocommerce' );
 24         $this->settings           = array(
 25             'title'  => array(
 26                 'type'  => 'text',
 27                 'std'   => __( 'Active Filters', 'woocommerce' ),
 28                 'label' => __( 'Title', 'woocommerce' )
 29             )
 30         );
 31         parent::__construct();
 32     }
 33 
 34     /**
 35      * widget function.
 36      *
 37      * @see WP_Widget
 38      * @access public
 39      * @param array $args
 40      * @param array $instance
 41      * @return void
 42      */
 43     public function widget( $args, $instance ) {
 44         global $_chosen_attributes, $woocommerce;
 45 
 46         extract( $args );
 47 
 48         if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) )
 49             return;
 50 
 51         $current_term   = is_tax() ? get_queried_object()->term_id : '';
 52         $current_tax    = is_tax() ? get_queried_object()->taxonomy : '';
 53         $title          = isset( $instance['title'] ) ? $instance['title'] : '';
 54         $title          = apply_filters( 'widget_title', $title, $instance, $this->id_base );
 55 
 56         // Price
 57         $min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : 0;
 58         $max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : 0;
 59 
 60         if ( count( $_chosen_attributes ) > 0 || $min_price > 0 || $max_price > 0 ) {
 61 
 62             echo $before_widget;
 63             if ( $title ) {
 64                 echo $before_title . $title . $after_title;
 65             }
 66 
 67             echo "<ul>";
 68 
 69             // Attributes
 70             if (!is_null($_chosen_attributes)){
 71                 foreach ( $_chosen_attributes as $taxonomy => $data ) {
 72 
 73                     foreach ( $data['terms'] as $term_id ) {
 74                         $term               = get_term( $term_id, $taxonomy );
 75                         $taxonomy_filter    = str_replace( 'pa_', '', $taxonomy );
 76                         $current_filter     = ! empty( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '';
 77                         $new_filter         = array_map( 'absint', explode( ',', $current_filter ) );
 78                         $new_filter         = array_diff( $new_filter, array( $term_id ) );
 79 
 80                         $link = remove_query_arg( 'filter_' . $taxonomy_filter );
 81 
 82                         if ( sizeof( $new_filter ) > 0 )
 83                             $link = add_query_arg( 'filter_' . $taxonomy_filter, implode( ',', $new_filter ), $link );
 84 
 85                         echo '<li class="chosen"><a title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . $term->name . '</a></li>';
 86                     }
 87                 }
 88             }
 89 
 90             if ( $min_price ) {
 91                 $link = remove_query_arg( 'min_price' );
 92                 echo '<li class="chosen"><a title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . __( 'Min', 'woocommerce' ) . ' ' . wc_price( $min_price ) . '</a></li>';
 93             }
 94 
 95             if ( $max_price ) {
 96                 $link = remove_query_arg( 'max_price' );
 97                 echo '<li class="chosen"><a title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . __( 'Max', 'woocommerce' ) . ' ' . wc_price( $max_price ) . '</a></li>';
 98             }
 99 
100             echo "</ul>";
101 
102             echo $after_widget;
103         }
104     }
105 }
106 
107 register_widget( 'WC_Widget_Layered_Nav_Filters' );
108 
WooCommerce API documentation generated by ApiGen 2.8.0