/lib/widgets/class-ss-wc-widget-layered-nav-filters.php

https://gitlab.com/aristath/shoestrap-3-woocommerce-child · PHP · 103 lines · 61 code · 19 blank · 23 comment · 11 complexity · 9fbce16be2b7c9ace1350ab96486aa66 MD5 · raw file

  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. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  12. class SS_WC_Widget_Layered_Nav_Filters extends WC_Widget {
  13. /**
  14. * Constructor
  15. */
  16. public function __construct() {
  17. $this->widget_cssclass = 'woocommerce widget_layered_nav_filters';
  18. $this->widget_description = __( 'Shows active layered nav filters so users can see and deactivate them.', 'woocommerce' );
  19. $this->widget_id = 'woocommerce_layered_nav_filters';
  20. $this->widget_name = __( 'WooCommerce Layered Nav Filters', 'woocommerce' );
  21. $this->settings = array(
  22. 'title' => array(
  23. 'type' => 'text',
  24. 'std' => __( 'Active Filters', 'woocommerce' ),
  25. 'label' => __( 'Title', 'woocommerce' )
  26. )
  27. );
  28. parent::__construct();
  29. }
  30. /**
  31. * widget function.
  32. *
  33. * @see WP_Widget
  34. * @access public
  35. * @param array $args
  36. * @param array $instance
  37. * @return void
  38. */
  39. public function widget( $args, $instance ) {
  40. global $_chosen_attributes, $woocommerce;
  41. extract( $args );
  42. if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) )
  43. return;
  44. $current_term = is_tax() ? get_queried_object()->term_id : '';
  45. $current_tax = is_tax() ? get_queried_object()->taxonomy : '';
  46. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  47. // Price
  48. $min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : 0;
  49. $max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : 0;
  50. if ( count( $_chosen_attributes ) > 0 || $min_price > 0 || $max_price > 0 ) {
  51. echo $before_widget;
  52. if ( $title ) {
  53. echo $before_title . $title . $after_title;
  54. }
  55. // Attributes
  56. if (!is_null($_chosen_attributes)){
  57. foreach ( $_chosen_attributes as $taxonomy => $data ) {
  58. foreach ( $data['terms'] as $term_id ) {
  59. $term = get_term( $term_id, $taxonomy );
  60. $taxonomy_filter = str_replace( 'pa_', '', $taxonomy );
  61. $current_filter = ! empty( $_GET[ 'filter_' . $taxonomy_filter ] ) ? $_GET[ 'filter_' . $taxonomy_filter ] : '';
  62. $new_filter = array_map( 'absint', explode( ',', $current_filter ) );
  63. $new_filter = array_diff( $new_filter, array( $term_id ) );
  64. $link = remove_query_arg( 'filter_' . $taxonomy_filter );
  65. if ( sizeof( $new_filter ) > 0 )
  66. $link = add_query_arg( 'filter_' . $taxonomy_filter, implode( ',', $new_filter ), $link );
  67. echo '<a class="chosen label primary label-default" title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . $term->name . ' <i class="el-icon-remove"></i></a> ';
  68. }
  69. }
  70. }
  71. if ( $min_price ) {
  72. $link = remove_query_arg( 'min_price' );
  73. echo '<a class="chosen label primary label-default" title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . __( 'Min', 'woocommerce' ) . ' ' . wc_price( $min_price ) . ' <i class="el-icon-remove"></i></a> ';
  74. }
  75. if ( $max_price ) {
  76. $link = remove_query_arg( 'max_price' );
  77. echo '<a class="chosen label primary label-default" title="' . __( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . __( 'Max', 'woocommerce' ) . ' ' . wc_price( $max_price ) . ' <i class="el-icon-remove"></i></a> ';
  78. }
  79. echo $after_widget;
  80. }
  81. }
  82. }
  83. unregister_widget( 'WC_Widget_Layered_Nav_Filters' );
  84. register_widget( 'SS_WC_Widget_Layered_Nav_Filters' );