Support Forums

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Range overlap

Viewing 7 posts - 1 through 7 (of 7 total)
  • P.
    #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 000

    Our 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?

    Trevor Moderator
    #228565

    I will need to refer this question to Ross, Patrick. Are you currently using v2.4.6?

    Are you able to send me a live link/URL to your search page so I can take a look?

    P.
    #231225
    This reply has been marked as private.
    Ross Moderator
    #231505
    This reply has been marked as private.
    Ross Moderator
    #231607

    Hi 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-object

    I’ve done a test, and here is the code you would need to affect your mileage field (copy to functions.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

    P.
    #232670

    Hi 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
    #233031

    Hi 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

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.