WC_Widget_Layered_Nav_Filters

Layered Navigation Filters Widget.

Defined (1)

The class is defined in the following location(s).

/includes/widgets/class-wc-widget-layered-nav-filters.php  
  1. class WC_Widget_Layered_Nav_Filters extends WC_Widget { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->widget_cssclass = 'woocommerce widget_layered_nav_filters'; 
  8. $this->widget_description = __( 'Shows active layered nav filters so users can see and deactivate them.', 'woocommerce' ); 
  9. $this->widget_id = 'woocommerce_layered_nav_filters'; 
  10. $this->widget_name = __( 'WooCommerce layered nav filters', 'woocommerce' ); 
  11. $this->settings = array( 
  12. 'title' => array( 
  13. 'type' => 'text',  
  14. 'std' => __( 'Active filters', 'woocommerce' ),  
  15. 'label' => __( 'Title', 'woocommerce' ),  
  16. ),  
  17. ); 
  18.  
  19. parent::__construct(); 
  20.  
  21. /** 
  22. * Get current page URL for layered nav items. 
  23. * @return string 
  24. */ 
  25. protected function get_page_base_url() { 
  26. if ( defined( 'SHOP_IS_ON_FRONT' ) ) { 
  27. $link = home_url(); 
  28. } elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) { 
  29. $link = get_post_type_archive_link( 'product' ); 
  30. } elseif ( is_product_category() ) { 
  31. $link = get_term_link( get_query_var( 'product_cat' ), 'product_cat' ); 
  32. } elseif ( is_product_tag() ) { 
  33. $link = get_term_link( get_query_var( 'product_tag' ), 'product_tag' ); 
  34. } else { 
  35. $queried_object = get_queried_object(); 
  36. $link = get_term_link( $queried_object->slug, $queried_object->taxonomy ); 
  37.  
  38. // Min/Max 
  39. if ( isset( $_GET['min_price'] ) ) { 
  40. $link = add_query_arg( 'min_price', wc_clean( $_GET['min_price'] ), $link ); 
  41.  
  42. if ( isset( $_GET['max_price'] ) ) { 
  43. $link = add_query_arg( 'max_price', wc_clean( $_GET['max_price'] ), $link ); 
  44.  
  45. // Orderby 
  46. if ( isset( $_GET['orderby'] ) ) { 
  47. $link = add_query_arg( 'orderby', wc_clean( $_GET['orderby'] ), $link ); 
  48.  
  49. /** 
  50. * Search Arg. 
  51. * To support quote characters, first they are decoded from " entities, then URL encoded. 
  52. */ 
  53. if ( get_search_query() ) { 
  54. $link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link ); 
  55.  
  56. // Post Type Arg 
  57. if ( isset( $_GET['post_type'] ) ) { 
  58. $link = add_query_arg( 'post_type', wc_clean( $_GET['post_type'] ), $link ); 
  59.  
  60. // Min Rating Arg 
  61. if ( isset( $_GET['rating_filter'] ) ) { 
  62. $link = add_query_arg( 'rating_filter', wc_clean( $_GET['rating_filter'] ), $link ); 
  63.  
  64. // All current filters 
  65. if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) { 
  66. foreach ( $_chosen_attributes as $name => $data ) { 
  67. $filter_name = sanitize_title( str_replace( 'pa_', '', $name ) ); 
  68. if ( ! empty( $data['terms'] ) ) { 
  69. $link = add_query_arg( 'filter_' . $filter_name, implode( ', ', $data['terms'] ), $link ); 
  70. if ( 'or' == $data['query_type'] ) { 
  71. $link = add_query_arg( 'query_type_' . $filter_name, 'or', $link ); 
  72.  
  73. return $link; 
  74.  
  75. /** 
  76. * Output widget. 
  77. * @see WP_Widget 
  78. * @param array $args 
  79. * @param array $instance 
  80. */ 
  81. public function widget( $args, $instance ) { 
  82. if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) { 
  83. return; 
  84.  
  85. $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes(); 
  86. $min_price = isset( $_GET['min_price'] ) ? wc_clean( $_GET['min_price'] ) : 0; 
  87. $max_price = isset( $_GET['max_price'] ) ? wc_clean( $_GET['max_price'] ) : 0; 
  88. $rating_filter = isset( $_GET['rating_filter'] ) ? array_filter( array_map( 'absint', explode( ', ', $_GET['rating_filter'] ) ) ) : array(); 
  89. $base_link = $this->get_page_base_url(); 
  90.  
  91. if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price || ! empty( $rating_filter ) ) { 
  92.  
  93. $this->widget_start( $args, $instance ); 
  94.  
  95. echo '<ul>'; 
  96.  
  97. // Attributes 
  98. if ( ! empty( $_chosen_attributes ) ) { 
  99. foreach ( $_chosen_attributes as $taxonomy => $data ) { 
  100. foreach ( $data['terms'] as $term_slug ) { 
  101. if ( ! $term = get_term_by( 'slug', $term_slug, $taxonomy ) ) { 
  102. continue; 
  103.  
  104. $filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) ); 
  105. $current_filter = isset( $_GET[ $filter_name ] ) ? explode( ', ', wc_clean( $_GET[ $filter_name ] ) ) : array(); 
  106. $current_filter = array_map( 'sanitize_title', $current_filter ); 
  107. $new_filter = array_diff( $current_filter, array( $term_slug ) ); 
  108.  
  109. $link = remove_query_arg( array( 'add-to-cart', $filter_name ), $base_link ); 
  110.  
  111. if ( sizeof( $new_filter ) > 0 ) { 
  112. $link = add_query_arg( $filter_name, implode( ', ', $new_filter ), $link ); 
  113.  
  114. echo '<li class="chosen"><a aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a></li>'; 
  115.  
  116. if ( $min_price ) { 
  117. $link = remove_query_arg( 'min_price', $base_link ); 
  118. echo '<li class="chosen"><a aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Min %s', 'woocommerce' ), wc_price( $min_price ) ) . '</a></li>'; 
  119.  
  120. if ( $max_price ) { 
  121. $link = remove_query_arg( 'max_price', $base_link ); 
  122. echo '<li class="chosen"><a aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Max %s', 'woocommerce' ), wc_price( $max_price ) ) . '</a></li>'; 
  123.  
  124. if ( ! empty( $rating_filter ) ) { 
  125. foreach ( $rating_filter as $rating ) { 
  126. $link_ratings = implode( ', ', array_diff( $rating_filter, array( $rating ) ) ); 
  127. $link = $link_ratings ? add_query_arg( 'rating_filter', $link_ratings ) : remove_query_arg( 'rating_filter', $base_link ); 
  128. echo '<li class="chosen"><a aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( esc_html__( 'Rated %s out of 5', 'woocommerce' ), esc_html( $rating ) ) . '</a></li>'; 
  129.  
  130. echo '</ul>'; 
  131.  
  132. $this->widget_end( $args );