Forums Forums Search Search Results for 'sf_input_object_pre function'

Viewing 10 results - 31 through 40 (of 106 total)
  • Author
    Search Results
  • #259039

    Trevor
    Participant

    What order would you like, or would it be entirely custom?

    You may need to use this filter and your own custom code to do this:

    https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    There are many snippets of code in this forum that use this filter. This forum search should show some of these:

    https://support.searchandfilter.com/forums/search/function+sf_input_object_pre/

    #258820

    Ross
    Keymaster

    I don’t have access to your site anymore, but this will replace all the optinos, with the 3 options you asked for:

    function update_field_options($input_object, $sfid){
    	if( $input_object['name'] != '_sfm__price' ) {
    		return $input_object;
    	}
    		
    	// if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
    	if( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// this is an array of all the options, we can remove them all, recreate them, etc
    	$new_options = array();
    	
    	// so create 3 options:
    	$option1 = new StdClass();
    	$option1->value = "0+100";
    	$option1->label = "0 - 100";
    	array_push( $new_options, $option1 );
    	
    	$option2 = new StdClass();
    	$option2->value = "100+250";
    	$option2->label = "100 - 250";
    	array_push( $new_options, $option2 );
    	
    	$option3 = new StdClass();
    	$option3->value = "250+10000000";
    	$option3->label = "250 kr and up";
    	array_push( $new_options, $option3 );
    	
    	$input_object['options'] = $new_options;
    
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);

    Thanks

    #258119

    Anonymous
    Inactive

    So, here is my code, don’t know how you want to make use for it, but here it is in case it helps someone!

    First of all, I just set a couple of values in the admin, 0-100 – it makes no difference since I am unsetting the initial options anyway:

    function change_car_engine_size($input_object, $sfid) {
    		if($input_object['name']=='_sfm_car_engine_size') {
    			unset($input_object['options']);
    			$input_object['options']=array();
    			$arr = ['0','100','200','300','400', '500', '600', '700', '800', '900', '1000','2500','3000','3500','4000'];
    			foreach ($arr as $value) {
    				$new_option = new StdClass();
    				$new_option->value = $value;
    				$new_option->label = number_format($value, 0, ',', '.');
    				array_push($input_object['options'], $new_option);
    			}
    
    		}
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'change_car_engine_size', 10, 2);
    #257605

    Ross
    Keymaster

    Hi Thomas

    I just did a test, this code, replaces the 4 th option in a list, with what you mention:

    function update_field_options($input_object, $sfid){
    	
    	// ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag'
    	if( $input_object['name'] != '_sfm_price' ) {
    		return $input_object;
    	}
    		
    	// if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
    	if( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// this is an array of all the options, we can remove them all, recreate them, etc
    	// var_dump( $input_object['options'] );
    	
    	// we know the option you want to replace is the 4th one in the list (including the default), so we can do :
    	if ( isset ( $input_object['options'][3] ) ) {
    		$new_option = new StdClass();
    		$new_option->value = "250+10000000";
    		$new_option->label = "250 kr and up";
    		$input_object['options'][3] = $new_option;
    	}
    	
    
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'update_field_options', 10, 2);

    That should get you going 🙂

    Read the comments for a few clues.

    Best


    Anonymous
    Inactive

    Found it by investigating the inspector, but I think the name of each field should be more clearly visible.

    Here is my working function:

    function top_search_additional_attributes($input_object, $sfid) {
    		if($input_object['name']=='_sf_search' && $sfid==161) {
    			$input_object['attributes']['autocomplete'] = 'off';
    			$input_object['attributes']['autocapitalize'] = 'off';
    			$input_object['attributes']['autocorrect'] = 'off';
    			$input_object['attributes']['spellcheck'] = 'false';
    			echo $sfid;
    		}
    
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'top_search_additional_attributes', 10, 2);
    #250604

    In reply to: Total Count


    Ross
    Keymaster

    Hi Maurino

    We have a WP / PHP hook which you can use to modify each individual option in your field – you could then loop through all your options, count the total results, and add that to the first option (All Items) – this is the filter: https://searchandfilter.com/documentation/action-filter-reference/#filter-input-object

    I’ve actually gone ahead and managed to create a code snippet for you that does this:

    function add_count_to_first_option( $input_object, $sfid ) {
    
    	// if the field doesn't have input options, then leave it alone
    	if ( ! isset( $input_object['options'] ) ) {
    		return $input_object;		
    	}
    
    	$total_count = 0;
    
    	// loop through each option and add up the total count
    	foreach ( $input_object['options'] as $option ) {
    		if ( isset( $option->count ) ) {
    			
    			$total_count += absint( $option->count );
    			
    		}
    	}
    
    	// now check to make sure there was a first option (with value of "")
    	// and update its count
    	if ( isset(  $input_object['options'][0] ) ) {
    		if ( '' === $input_object['options'][0]->value ) {
    			$input_object['options'][0]->label .= ' (' . $total_count . ')';
    		}
    	}
    
    	return $input_object;
    }
    add_filter('sf_input_object_pre', 'add_count_to_first_option', 10, 2);
    

    Add this to your themes functions.php, and that will add a count to all first options in your fields.

    Thanks

    #250388

    Anonymous
    Inactive

    Hi Ross,

    Thanks for checking!
    The result in your screenshot is recently added bij ACF as a test, and always working indeed because they hardcoded a field name and a post id in it:

    var_dump(get_field_object('#!#etrtodiameter','314322'));

    When you look at the html attribute “title” from the filter “ISO diameter” below your screenshot, and you refresh several times, you will see that it is sometimes filled and sometimes not. This attribute is being picked up by this hook in functions.php:


    function filter_function_name($input_object, $sfid)
    {
    $acf_name = substr($input_object[‘name’],5);
    //echo ‘ACF-name: ‘ . $acf_name . ‘. ‘;
    $field = get_field_object($acf_name); // As a test I added 14313 as a product taxonomy id (2nd parameter), this seems to have no effect (still randomly echoing instructions or not)
    $instruction = $field[‘instructions’];
    //echo ‘instruction: ‘ . $instruction . ‘. ‘;
    //echo ‘field_object: ‘ . get_field_object($acf_name) . ‘. ‘;
    if(strlen($instruction)>0)
    {
    $input_object[‘attributes’][‘title’] = $instruction;
    }

    return $input_object;

    }
    add_filter(‘sf_input_object_pre’, ‘filter_function_name’, 10, 2);


    So the question is, how can the stability of the first code snippet be integrated in the second code snippet, while keeping it dynamic; custom fields can be related to multiple taxonomy id’s and post id’s and I never know what products are being shown. Or, while typing, I wonder if a solution could be to pick the first product in a filter and pass the id as and argument for the function in the second code snippet. What do you think? And if this is a good idea, do you have an idea how to build this efficiently?

    Thanks too for your suggestion about the shortcode/front end loading issue. How can I achieve this when using a custom, dynamically used, template?

    #250212

    Anonymous
    Inactive

    Hi Trevor,

    The hook that you helped me with this week, for adding ACF-fields to filters is working randomly; sometimes it returns the wanted info, sometimes it is empty. I contacted ACF about it and this is their (first) answer: “since you are on a taxonomy archive, I believe passing a second parameter containing a post id would be very helpful to ensure that the get_field_object always retrieves data from this post.”.

    I think in that case that I have to pass the post id from the filter, or am I wrong? Do you have a suggestion how to achieve this?

    The hook:

    function filter_function_name($input_object, $sfid)
    {
        $acf_name = substr($input_object['name'],5);
        //echo 'ACF-name: ' . $acf_name . '. ';
        $instruction = get_field_object($acf_name)['instructions'];
        //echo 'instruction: ' . $instruction . '. ';
        //echo 'field_object: ' . get_field_object($acf_name) . '. ';
    	if(strlen($instruction)>0)
    	{
            $input_object['attributes']['title'] = $instruction;
    	}
    	
        return $input_object;
        
    }
    add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);

    Thanks!

    #249752

    In reply to: Add info button


    Anonymous
    Inactive

    Ok nice, this is working:

    function filter_function_name($input_object, $sfid)
    {
        $acf_name = substr($input_object['name'],5);
        
    	if(strlen($acf_name)>0)
    	{
    		$input_object['attributes']['title'] = get_field_object($acf_name)['instructions'];
    	}
    	
        return $input_object;
        
    }
    add_filter('sf_input_object_pre', 'filter_function_name', 10, 2);

    It only gives different attribute names based on the field type, for example “title” and “class title” but I wil fix that with JS.

    Thanks a lot!

    #245284

    In reply to: ajax is not working


    Ross
    Keymaster

    This is coming as an option in the admin area in S&F version 3.

    For now you can do it with a PHP filter (add this to your child theme’s functions.php file):

    function sf_remove_first_option($input_object, $sfid){
    	
    	// ensure we are only filtering the correct field name - in this case the field we want to filter has the name '_sft_post_tag'
    	if( $input_object['name'] != '_sft_post_tag' ) {
    		return $input_object;
    	}
    		
    	// if we want to filter the options generated, we need to make sure the options variable actually exists before proceeding (its only available to certain field types)
    	if( ! isset( $input_object['options'] ) ) {
    		return $input_object;
    	}
    	
    	// now we know we have options, remove the first option
    	array_shift( $input_object['options'] ); 
    
    	return $input_object;
    }
    
    add_filter('sf_input_object_pre', 'sf_remove_first_option', 10, 2);

    Change _sft_post_tag for the name of your field.

    Thanks

Viewing 10 results - 31 through 40 (of 106 total)