• Resolved peti73

    (@peti73)


    Hi,

    first of all thank You for this great plugin!

    I’d need help with the following:
    how can i use a html field value (or shortcode value) in a calculation?
    I have a shortcode which get an actual metal price (via metals-api). I was able to put that shortcode in a html field with this method: using shortcode in forminator, and it displays the price correctly.

    But then how can i use this field value in a calculation?
    Is there a code or workaround to do this?

    Thank You in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello @peti73 !

    Hope you’re having a good week!

    Glad to hear you’re finding Forminator useful!

    As this is not possible out of the box, I’ve asked our Second Line Support team to check if this can be achieved using a code snippet to place the value in the form for further use. We’ll update you here as soon as we hear back from them.

    Kind regards,
    Pawel

    Thread Starter peti73

    (@peti73)

    Thanks for the information!

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @peti73

    I hope you are doing well.

    Our developers created small code to help you:

    <?php
    
    add_filter( 'forminator_field_calculation_markup', 'wpmudev_render_calc_value', 10, 4 );
    function wpmudev_render_calc_value( $html, $id, $required, $value ){
    	if( $id == 'calculation-1-field' ){
    		if( strpos( $html, 'data-formula=""') !== false ){
    			$html = str_replace('data-formula=""', 'data-formula="15*1"', $html);
    		}
    	}
    	return $html;
    }
    
    add_filter( 'forminator_field_calculation_calculable_value', 'wpmudev_render_calc_value_db', 10, 3 );
    function wpmudev_render_calc_value_db( $formula, $prepared_data, $field_settings ){
    	if( $prepared_data['form_id'] != 361 ){
    		return $formula;
    	}
    	
    	if( $field_settings['element_id'] == 'calculation-1' ){
    		$formula = '15*1';
    	}
    
    	return $formula;
    }

    You can add the code as mu-plugin following this guide https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    calculation-1-field , calculation-1 should be changed as per the field settings. Form ID should be changed from 361 to your form ID. The developer used this 15*1 which should be changed to the value that you get via a shortcode from an external API.

    Best Regards
    Patrick Freitas

    Thread Starter peti73

    (@peti73)

    Hi Patrick,

    unfortunately i cannot follow you.
    I don’t have a “calculation-1-field” – is that the html field which displays the shortcode’s value?
    What formula should i put in the “calculation-1” element? Without a formula a cannot add that field.
    How do i change the “15*1” to my shortcode value if i don’t know the value, because it will received via that shortcode? Or should i put my shortcode here?

    I’m sorry I don’t understand.

    Hello @peti73 !

    Apologies for the confusion here!

    To use the snippet, you will need to add an additional Calculations field (you can make it hidden) which will take the value from the shortcode and you will be able to use it for further calculations in the other field. The HTML field will only be used in this case to display the value to the visitor.

    So calculation-1-field is the new Calculations hidden field you add for this purpose.

    In the field you can add a formula such as 0 + 0 just so it has something there.

    For the 15*1 please change:

    $formula = '15*1';

    To something like:

    $formula = do_shortcode('[your_shortcode]') . '+ 0';

    Best regards,
    Pawel

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @peti73 ,

    We haven’t heard from you for several days now, so it looks like you don’t have any more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Using html field/shortcode value in calculation’ is closed to new replies.