1 <?php
2 3 4 5 6 7 8 9 10 11 12
13
14 if ( ! defined( 'ABSPATH' ) ) exit;
15
16 class WC_Widget_Price_Filter extends WC_Widget {
17
18 19 20
21 public function __construct() {
22 $this->widget_cssclass = 'woocommerce widget_price_filter';
23 $this->widget_description = __( 'Shows a price filter slider in a widget which lets you narrow down the list of shown products when viewing product categories.', 'woocommerce' );
24 $this->widget_id = 'woocommerce_price_filter';
25 $this->widget_name = __( 'WooCommerce Price Filter', 'woocommerce' );
26 $this->settings = array(
27 'title' => array(
28 'type' => 'text',
29 'std' => __( 'Filter by price', 'woocommerce' ),
30 'label' => __( 'Title', 'woocommerce' )
31 )
32 );
33 parent::__construct();
34 }
35
36 37 38 39 40 41 42 43 44
45 public function widget( $args, $instance ) {
46 global $_chosen_attributes, $wpdb, $woocommerce, $wp_query, $wp;
47
48 extract( $args );
49
50 if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) )
51 return;
52
53 if ( sizeof( WC()->query->unfiltered_product_ids ) == 0 )
54 return;
55
56 $min_price = isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : '';
57 $max_price = isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : '';
58
59 wp_enqueue_script( 'wc-price-slider' );
60
61 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
62
63
64 $fields = '';
65
66 if ( get_search_query() )
67 $fields .= '<input type="hidden" name="s" value="' . get_search_query() . '" />';
68
69 if ( ! empty( $_GET['post_type'] ) )
70 $fields .= '<input type="hidden" name="post_type" value="' . esc_attr( $_GET['post_type'] ) . '" />';
71
72 if ( ! empty ( $_GET['product_cat'] ) )
73 $fields .= '<input type="hidden" name="product_cat" value="' . esc_attr( $_GET['product_cat'] ) . '" />';
74
75 if ( ! empty( $_GET['product_tag'] ) )
76 $fields .= '<input type="hidden" name="product_tag" value="' . esc_attr( $_GET['product_tag'] ) . '" />';
77
78 if ( ! empty( $_GET['orderby'] ) )
79 $fields .= '<input type="hidden" name="orderby" value="' . esc_attr( $_GET['orderby'] ) . '" />';
80
81 if ( $_chosen_attributes ) foreach ( $_chosen_attributes as $attribute => $data ) {
82
83 $taxonomy_filter = 'filter_' . str_replace( 'pa_', '', $attribute );
84
85 $fields .= '<input type="hidden" name="' . esc_attr( $taxonomy_filter ) . '" value="' . esc_attr( implode( ',', $data['terms'] ) ) . '" />';
86
87 if ( $data['query_type'] == 'or' )
88 $fields .= '<input type="hidden" name="' . esc_attr( str_replace( 'pa_', 'query_type_', $attribute ) ) . '" value="or" />';
89 }
90
91 $min = $max = 0;
92 $post_min = $post_max = '';
93
94 if ( sizeof( WC()->query->layered_nav_product_ids ) === 0 ) {
95 $min = floor( $wpdb->get_var(
96 $wpdb->prepare('
97 SELECT min(meta_value + 0)
98 FROM %1$s
99 LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
100 WHERE ( meta_key = \'%3$s\' OR meta_key = \'%4$s\' )
101 AND meta_value != ""
102 ', $wpdb->posts, $wpdb->postmeta, '_price', '_min_variation_price' )
103 ) );
104 $max = ceil( $wpdb->get_var(
105 $wpdb->prepare('
106 SELECT max(meta_value + 0)
107 FROM %1$s
108 LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
109 WHERE meta_key = \'%3$s\'
110 ', $wpdb->posts, $wpdb->postmeta, '_price' )
111 ) );
112 } else {
113 $min = floor( $wpdb->get_var(
114 $wpdb->prepare('
115 SELECT min(meta_value + 0)
116 FROM %1$s
117 LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
118 WHERE ( meta_key =\'%3$s\' OR meta_key =\'%4$s\' )
119 AND meta_value != ""
120 AND (
121 %1$s.ID IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
122 OR (
123 %1$s.post_parent IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
124 AND %1$s.post_parent != 0
125 )
126 )
127 ', $wpdb->posts, $wpdb->postmeta, '_price', '_min_variation_price'
128 ) ) );
129 $max = ceil( $wpdb->get_var(
130 $wpdb->prepare('
131 SELECT max(meta_value + 0)
132 FROM %1$s
133 LEFT JOIN %2$s ON %1$s.ID = %2$s.post_id
134 WHERE meta_key =\'%3$s\'
135 AND (
136 %1$s.ID IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
137 OR (
138 %1$s.post_parent IN (' . implode( ',', array_map( 'absint', WC()->query->layered_nav_product_ids ) ) . ')
139 AND %1$s.post_parent != 0
140 )
141 )
142 ', $wpdb->posts, $wpdb->postmeta, '_price'
143 ) ) );
144 }
145
146 if ( $min == $max )
147 return;
148
149 echo $before_widget . $before_title . $title . $after_title;
150
151 if ( get_option( 'permalink_structure' ) == '' )
152 $form_action = remove_query_arg( array( 'page', 'paged' ), add_query_arg( $wp->query_string, '', home_url( $wp->request ) ) );
153 else
154 $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( $wp->request ) );
155
156 echo '<form method="get" action="' . esc_attr( $form_action ) . '">
157 <div class="price_slider_wrapper">
158 <div class="price_slider" style="display:none;"></div>
159 <div class="price_slider_amount">
160 <input type="text" id="min_price" name="min_price" value="' . esc_attr( $min_price ) . '" data-min="'.esc_attr( $min ).'" placeholder="'.__('Min price', 'woocommerce' ).'" />
161 <input type="text" id="max_price" name="max_price" value="' . esc_attr( $max_price ) . '" data-max="'.esc_attr( $max ).'" placeholder="'.__( 'Max price', 'woocommerce' ).'" />
162 <button type="submit" class="button">'.__( 'Filter', 'woocommerce' ).'</button>
163 <div class="price_label" style="display:none;">
164 '.__( 'Price:', 'woocommerce' ).' <span class="from"></span> — <span class="to"></span>
165 </div>
166 ' . $fields . '
167 <div class="clear"></div>
168 </div>
169 </div>
170 </form>';
171
172 echo $after_widget;
173 }
174 }
175
176 register_widget( 'WC_Widget_Price_Filter' );