• Resolved Reventlov

    (@reventlov)


    Hello,

    I use calculation function in forminator.

    I have a long select list with several options. Each options will be used in a calculation.

    There is a csv import function for select options (with fields ‘label’, ‘value’, ‘selected’ and ‘image url’). But for calculation function, it seems i have to enter calculation value for each options manually. It is very not practical. Is there a way to add the calculation value in the csv ?

    Regards.

    • This topic was modified 1 year, 6 months ago by Reventlov.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @reventlov

    I hope you’re well today and thank you for your question!

    Currently it’s not possible out of the box – only options can be imported this way.

    We have a custom code to populate select field (including calculation values for options) from custom posts so I think there is a chance it could be modified to use CSV file instead.

    However, I need to consult it with our developers first so no promises yet. I have already asked our Forminator Team to check it and we (I or one of my colleagues) will get back to you here once we got feedback on this.

    Please keep an eye on this ticket.

    Best regards,
    Adam

    Thread Starter Reventlov

    (@reventlov)

    Hi Adam,

    Thank you.

    Can you tell me more about the custom code to populate select field (including calculation values for options) from custom posts??

    I could be interested in that too.

    Regards.

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @reventlov

    The code works by fetching posts of defined custom post type and setting select field options this way:

    – post title becomes option label
    – post ID becomes option value
    – and the calculation value is set based on associated custom field (created with ACF plugin)

    So in short, per each select field you should have a post of custom type with its title set to what the option label should and calculation value added as ACF custom field value.

    The post type and the custom field name can be defined in the code directly so it would fetch it from correct ones. Here is the code (should be added to site Must Use plugin):

    https://gist.github.com/adczk/d488c7a69b8ee6223303267583fd01c3

    Best regards,
    Adam

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @reventlov

    I just got response from our developers and another version of code for you. This one should work fine with data taken right from the CSV file.

    The code is:

    <?php 
    add_filter(
    	'forminator_cform_render_fields',
    	function ( $wrappers, $model_id ) {
            
            if ( $model_id != 6 ) {
                return $wrappers;
            }
            
    		$csv_map = array(
    			'select-1' => WPMU_PLUGIN_DIR . '/sheet1.csv',
    		);
    
    		foreach ( $wrappers as $wrapper_key => $wrapper ) {
    			if ( ! isset( $wrapper['fields'] ) ) {
    				continue;
    			}
    
    			if (
    				in_array( $wrapper['fields'][0]['element_id'], array_keys( $csv_map ) ) &&
    				isset( $wrappers[ $wrapper_key ]['fields'][0]['options'] )
    			) {
    				$csv_path = $csv_map[ $wrapper['fields'][0]['element_id'] ];
    
    				if ( ! file_exists( $csv_path ) ) {
    					continue;
    				}
    
    				$file        = fopen( $csv_path, 'r' );
    				$new_options = array();
                    $opt_data = array();
    				while ( ( $line = fgetcsv( $file ) ) !== false ) {
    					$new_options[] = array(
    						'label' => sanitize_text_field( $line[0] ),
    						'value' => sanitize_text_field( $line[1] ),
    						'limit' => '',
    						'key'   => forminator_unique_key(),
                            'calculation' => sanitize_text_field( $line[2] ),
    					);
                        $opt_data['options'] = $new_options;
    				}
    				fclose( $file );
    				$select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][0]['element_id'], true );
    				if ( $select_field ) {
                        Forminator_API::update_form_field( $model_id, $wrapper['fields'][0]['element_id'], $opt_data );
                        $wrappers[ $wrapper_key ]['fields'][0]['options'] = $new_options;
    				}
    			}
    		}
    
    		return $wrappers;
    	},
    	10,
    	2
    );

    It should be added as Must Use plugin and you would also need to:

    1. replace number 6 in this line

    if ( $model_id != 6 ) {

    with an ID of your form (form ID is the number you see in form’s shortcode)

    2. in this part you define which select field should be filled-in and what’s the file name:

    $csv_map = array(
    			'select-1' => WPMU_PLUGIN_DIR . '/sheet1.csv',
    		);

    3. and the file (by default named “sheet1.csv”) should be uploaded

    – either to the same /wp-content/mu-plugins folder
    – or you can change the file path in the same like where you set file name by replacing WPMU_PLUGIN_DIR with the path

    The CSV file structure to be used would be quite simple (no header line!)

    New option 1, new-option-1, 123
    New option 2, new-option-2, 99

    and so on where the first column is option label, second is option value and third is calculation value (note: if you use decimals there, you cannot use comma separated format, so 10,99 may not work but 10.99 will).

    Best regards,
    Adam

    Thread Starter Reventlov

    (@reventlov)

    Hi Adam,

    Thank you very much. I will try that next week and keep you updated.

    Have a nice weekend.

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @wpmudev-support8 ,
    ?
    We haven’t heard from you for several days now, so it looks like you no longer need our assistance.
    ?
    Feel free to re-open this ticket if needed.
    ?
    Kind regards
    Kasia

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘csv import with calculation values’ is closed to new replies.