Forums › Forums › Search & Filter Pro › Range overlap
- This topic has 6 replies, 2 voices, and was last updated 4 years, 9 months ago by Ross.
-
Anonymous(Private) December 6, 2019 at 1:35 pm #228560
Hi Trevor,
I essentially have the exact same question as this gentleman had in 2015, but the topic seemed to lack a reply; https://support.searchandfilter.com/forums/topic/number-ranges-overlap/
I’m using a range filter with steps of 50 000, resulting in a filter like this;
0 – 50 000
50 000 – 100 000Our client doesn’t want any items to overlap in the filter, so ideally the first filter would end at 49 999, and the next step would be 50 000 – 99 999. Is there any way to accomplish this?
Ross Moderator(Private) January 20, 2020 at 6:42 pm #231607Hi Patrick
It’s not possible within the UI, but you can use a filter to manually update a fields options (and values):
https://searchandfilter.com/documentation/action-filter-reference/#filter-input-objectI’ve done a test, and here is the code you would need to affect your
mileage
field (copy tofunctions.php
in your child theme):function update_range_field_mileage($input_object, $sfid) { //make sure we affect the '_sfm_mileage' field only if($input_object['name']=='_sfm_mileage') { //udpate this field before rendering //all the options are stored in <code>$input_object['options']</code> as an array $new_options = array(); //create a new "default" option $new_option = new StdClass(); $new_option->value = ""; $new_option->label = "KM Stand"; array_push($new_options, $new_option); //create a new range option we want to add $new_option = new StdClass(); $new_option->value = "0+49999"; //this is the value that goes in the URL, which affects the query $new_option->label = "0 - 49999"; //the label can be anything array_push($new_options, $new_option);//create a new range option we want to add $new_option = new StdClass(); $new_option->value = "50000+99999"; //this is the value that goes in the URL, which affects the query $new_option->label = "50000 - 99999"; //the label can be anything array_push($new_options, $new_option); $new_option = new StdClass(); $new_option->value = "100000+150000"; //this is the value that goes in the URL, which affects the query $new_option->label = "100000 - 150000"; //the label can be anything array_push($new_options, $new_option); //now replace the options with our own custom options: $input_object['options'] = $new_options; } return $input_object; } add_filter('sf_input_object_pre', 'update_range_field_mileage', 10, 2);
Let me know how you get on.
Thanks
Anonymous(Private) January 31, 2020 at 8:00 am #232670Hi Ross,
A bit late with my reply, but this project was postponed for a while.
Thank you for the example code, that works like a charm!Is the addition of an option to prevent overlapping values something that’s on the roadmap for Search and Filter?
Your code snippet works brilliantly for now, but in case we would need a more dynamic option in the future it would be nice to know if it’s being considered 🙂
Ross Moderator(Private) February 4, 2020 at 3:11 pm #233031Hi Patrick
It’s under consideration, but right now as this is pretty much the only request it will be low on our to do list (and unfortunately that is huge!)
It’s possible to adapt the above code to be dynmaic, I’m sure if you need in the future I can write something.
Thanks
-
AuthorPosts