From 01696d92b09d54c40f47bd86aa89a70fe08df695 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 23 Jan 2019 17:34:51 +0000 Subject: [PATCH 1/8] Add comments and round to 10 --- assets/js/frontend/price-slider.js | 1 + includes/widgets/class-wc-widget-price-filter.php | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/assets/js/frontend/price-slider.js b/assets/js/frontend/price-slider.js index 682a2255d6f..aedd6c7aba8 100644 --- a/assets/js/frontend/price-slider.js +++ b/assets/js/frontend/price-slider.js @@ -41,6 +41,7 @@ jQuery( function( $ ) { animate: true, min: min_price, max: max_price, + step: 10, values: [ current_min_price, current_max_price ], create: function() { diff --git a/includes/widgets/class-wc-widget-price-filter.php b/includes/widgets/class-wc-widget-price-filter.php index 0949c7518dc..208bdfe56f4 100644 --- a/includes/widgets/class-wc-widget-price-filter.php +++ b/includes/widgets/class-wc-widget-price-filter.php @@ -70,17 +70,20 @@ class WC_Widget_Price_Filter extends WC_Widget { $min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : null; // WPCS: input var ok, CSRF ok. $max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : null; // WPCS: input var ok, CSRF ok. - if ( ! wc()->query->get_main_query()->post_count && null === $min_price && null === $max_price ) { + + // If there are not posts and we're not filtering, hide the widget. + if ( ! WC()->query->get_main_query()->post_count && null === $min_price && null === $max_price ) { return; } wp_enqueue_script( 'wc-price-slider' ); - // Find min and max price in current result set. + // Find min and max price in current result set, rounding to the nearest 10. $prices = $this->get_filtered_price(); - $min = floor( $prices->min_price ); - $max = ceil( $prices->max_price ); + $min = floor( $prices->min_price / 10 ) * 10; + $max = ceil( $prices->max_price / 10 ) * 10; + // If both min and max are equal, we don't need a slider. if ( $min === $max ) { return; } From b9de20ed275359262a676f1736ad401b4873d1ce Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 23 Jan 2019 15:31:26 +0000 Subject: [PATCH 2/8] Exclude paged from price slider --- includes/widgets/class-wc-widget-price-filter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/widgets/class-wc-widget-price-filter.php b/includes/widgets/class-wc-widget-price-filter.php index 208bdfe56f4..ac27a5948ed 100644 --- a/includes/widgets/class-wc-widget-price-filter.php +++ b/includes/widgets/class-wc-widget-price-filter.php @@ -109,7 +109,7 @@ class WC_Widget_Price_Filter extends WC_Widget { - ' . wc_query_string_form_fields( null, array( 'min_price', 'max_price' ), '', true ) . ' + ' . wc_query_string_form_fields( null, array( 'min_price', 'max_price', 'paged' ), '', true ) . '
From 3a94fc6d00001f8952bf7a69a96d899599e49929 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 23 Jan 2019 18:29:33 +0000 Subject: [PATCH 3/8] Introduce steps and cleanup variable names for clarity --- assets/js/frontend/price-slider.js | 7 +++-- .../widgets/class-wc-widget-price-filter.php | 29 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/assets/js/frontend/price-slider.js b/assets/js/frontend/price-slider.js index aedd6c7aba8..3b89b117c8b 100644 --- a/assets/js/frontend/price-slider.js +++ b/assets/js/frontend/price-slider.js @@ -31,8 +31,9 @@ jQuery( function( $ ) { $( 'input#min_price, input#max_price' ).hide(); $( '.price_slider, .price_label' ).show(); - var min_price = $( '.price_slider_amount #min_price' ).data( 'min' ), - max_price = $( '.price_slider_amount #max_price' ).data( 'max' ), + var min_price = $( '.price_slider_amount #min_price' ).data( 'min' ), + max_price = $( '.price_slider_amount #max_price' ).data( 'max' ), + step = $( '.price_slider_amount' ).data( 'step' ) || 1, current_min_price = $( '.price_slider_amount #min_price' ).val(), current_max_price = $( '.price_slider_amount #max_price' ).val(); @@ -41,7 +42,7 @@ jQuery( function( $ ) { animate: true, min: min_price, max: max_price, - step: 10, + step: step, values: [ current_min_price, current_max_price ], create: function() { diff --git a/includes/widgets/class-wc-widget-price-filter.php b/includes/widgets/class-wc-widget-price-filter.php index ac27a5948ed..86f8f75833a 100644 --- a/includes/widgets/class-wc-widget-price-filter.php +++ b/includes/widgets/class-wc-widget-price-filter.php @@ -68,23 +68,27 @@ class WC_Widget_Price_Filter extends WC_Widget { return; } - $min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : null; // WPCS: input var ok, CSRF ok. - $max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : null; // WPCS: input var ok, CSRF ok. + // Current active min and max price is retrieved from the query string. + $current_min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : null; // WPCS: input var ok, CSRF ok. + $current_max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : null; // WPCS: input var ok, CSRF ok. // If there are not posts and we're not filtering, hide the widget. - if ( ! WC()->query->get_main_query()->post_count && null === $min_price && null === $max_price ) { + if ( ! WC()->query->get_main_query()->post_count && null === $current_min_price && null === $current_max_price ) { return; } wp_enqueue_script( 'wc-price-slider' ); - // Find min and max price in current result set, rounding to the nearest 10. - $prices = $this->get_filtered_price(); - $min = floor( $prices->min_price / 10 ) * 10; - $max = ceil( $prices->max_price / 10 ) * 10; + // Find min and max price in current result set. + $prices = $this->get_filtered_price(); + $min_price = apply_filters( 'woocommerce_price_filter_widget_min_amount', floor( $prices->min_price / 10 ) * 10 ); + $max_price = apply_filters( 'woocommerce_price_filter_widget_max_amount', ceil( $prices->max_price / 10 ) * 10 ); + $current_min_price = is_null( $current_min_price ) ? $min_price : floor( $current_min_price / 10 ) * 10; + $current_max_price = is_null( $current_max_price ) ? $max_price : ceil( $current_max_price / 10 ) * 10; + $step = apply_filters( 'woocommerce_price_filter_widget_step', 10 ); // If both min and max are equal, we don't need a slider. - if ( $min === $max ) { + if ( $min_price === $max_price ) { return; } @@ -96,15 +100,12 @@ class WC_Widget_Price_Filter extends WC_Widget { $form_action = preg_replace( '%\/page/[0-9]+%', '', home_url( trailingslashit( $wp->request ) ) ); } - $min_price = null !== $min_price ? $min_price : apply_filters( 'woocommerce_price_filter_widget_min_amount', $min ); - $max_price = null !== $max_price ? $max_price : apply_filters( 'woocommerce_price_filter_widget_max_amount', $max ); - echo '
-
- - +
+ +