/widgets/widget-layered_nav_filters.php

https://github.com/markjaquith/woocommerce · PHP · 109 lines · 54 code · 25 blank · 30 comment · 11 complexity · 0d0ed81008ed9e50d2475c766092cb87 MD5 · raw file

  1. <?php
  2. /**
  3. * Layered Navigation Fitlers Widget
  4. *
  5. * @author WooThemes
  6. * @category Widgets
  7. * @package WooCommerce/Widgets
  8. * @version 1.7.0
  9. * @extends WP_Widget
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  12. class WooCommerce_Widget_Layered_Nav_Filters extends WP_Widget {
  13. var $woo_widget_cssclass;
  14. var $woo_widget_description;
  15. var $woo_widget_idbase;
  16. var $woo_widget_name;
  17. /**
  18. * constructor
  19. *
  20. * @access public
  21. * @return void
  22. */
  23. function WooCommerce_Widget_Layered_Nav_Filters() {
  24. /* Widget variable settings. */
  25. $this->woo_widget_cssclass = 'woocommerce widget_layered_nav_filters';
  26. $this->woo_widget_description = __( 'Shows active layered nav filters so users can see and deactivate them.', 'woocommerce' );
  27. $this->woo_widget_idbase = 'woocommerce_layered_nav_filters';
  28. $this->woo_widget_name = __( 'WooCommerce Layered Nav Filters', 'woocommerce' );
  29. /* Widget settings. */
  30. $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
  31. /* Create the widget. */
  32. $this->WP_Widget( 'woocommerce_layered_nav_filters', $this->woo_widget_name, $widget_ops );
  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. function widget( $args, $instance ) {
  44. global $_chosen_attributes, $woocommerce, $_attributes_array;
  45. extract( $args );
  46. if ( ! is_post_type_archive( 'product' ) && ! is_tax( array_merge( $_attributes_array, array( 'product_cat', 'product_tag' ) ) ) )
  47. return;
  48. $current_term = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->term_id : '';
  49. $current_tax = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->taxonomy : '';
  50. $title = __( 'Active filters', 'woocommerce' );
  51. //$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base );
  52. // Price
  53. $post_min = isset( $woocommerce->session->min_price ) ? $woocommerce->session->min_price : 0;
  54. $post_max = isset( $woocommerce->session->max_price ) ? $woocommerce->session->max_price : 0;
  55. if ( count( $_chosen_attributes ) > 0 || $post_min > 0 || $post_max > 0 ) {
  56. echo $before_widget . $before_title . $title . $after_title;
  57. echo "<ul>";
  58. // Attributes
  59. foreach ( $_chosen_attributes as $taxonomy => $data ) {
  60. foreach ( $data['terms'] as $term_id ) {
  61. $term = get_term( $term_id, $taxonomy );
  62. $taxonomy_filter = str_replace( 'pa_', '', $taxonomy );
  63. $current_filter = ! empty( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '';
  64. $new_filter = array_map( 'absint', explode( ',', $current_filter ) );
  65. $new_filter = array_diff( $new_filter, array( $term_id ) );
  66. $link = remove_query_arg( 'filter_' . $taxonomy_filter );
  67. if ( sizeof( $new_filter ) > 0 )
  68. $link = add_query_arg( 'filter_' . $taxonomy_filter, implode( ',', $new_filter ), $link );
  69. echo '<li><a title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . $link . '">' . $term->name . '</a></li>';
  70. }
  71. }
  72. if ( $post_min ) {
  73. $link = remove_query_arg( 'min_price' );
  74. echo '<li><a title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . $link . '">' . __( 'Min', 'woocommerce' ) . ' ' . woocommerce_price( $post_min ) . '</a></li>';
  75. }
  76. if ( $post_max ) {
  77. $link = remove_query_arg( 'max_price' );
  78. echo '<li><a title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . $link . '">' . __( 'Max', 'woocommerce' ) . ' ' . woocommerce_price( $post_max ) . '</a></li>';
  79. }
  80. echo "</ul>";
  81. echo $after_widget;
  82. }
  83. }
  84. }