• Resolved acrspf

    (@acrspf)


    Hi

    How would I pull existing data from an ACF field and put it into Calculations?

    What I have is a site that displays posts that have ACF field data attached to them. I have a form on each post that is pulling data from those ACF fields using a shortcode generator, that works fine for displaying the data, but how would I insert it into a Calculations field?

    Thank you …

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 21 total)
  • Thread Starter acrspf

    (@acrspf)

    More information: I’m getting the ACF field via shortcode and putting the shortcode into an html field for form display.

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @acrspf

    Hope you’re doing well today! Thank you for reaching out to us.

    I am afraid, there isn’t such workflow supported on the calculation field out of the box in Forminator.

    However, I have pinged our SLS team so that they can review your query and see what can be possible in this case. We will post an update here as soon as more information is available.

    Kind Regards,
    Saurabh

    Thread Starter acrspf

    (@acrspf)

    Thank you Saurabh. I hope so, otherwise we will have to find a different solution and we’d much prefer Forminator. Looking forward to your response.

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @acrspf

    Hope you’re doing well today!

    Our SLS team has shared the below custom Snippet as a workaround.

    <?php
    add_action( 'wp_footer', 'wpmudev_calc_field_post_meta', 9999 );
    function wpmudev_calc_field_post_meta() {
        global $post;
        if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'forminator_form' ) ) {
            return;
        }
    
        $calc_val = get_post_meta( $post->ID, 'number_1', true );
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($){
            setTimeout(function() {
                $('.forminator-custom-form').trigger('after.load.forminator');
            },100);
    
            $(document).on('after.load.forminator', function( e, form_id ) {
                if ( e.target.id == 'forminator-module-6' ) { // Please change 6 to your form's ID.
                    var calc_val = '<?php echo get_post_meta( $post->ID, 'number_1', true );?>';
                    setTimeout(function(){
                        $('#calculation-1 input').val(calc_val).trigger('change');
                    },1000);
                }
            });
        });
        </script>
        <?php
    }
    
    add_filter( 'forminator_field_calculation_calculable_value', 'wpmudev_calc_field_meta_val', 10, 3 );
    function wpmudev_calc_field_meta_val( $formula, $prepared_data, $field_settings ) {
        if ( $prepared_data['form_id'] != 6 ) { // Please change the form ID.
            return $formula;
        }
    
        if ( $field_settings['element_id'] == 'calculation-1' ) {
            $formula = get_post_meta( forminator_get_post_data( 'ID' ), 'number_1', true );
        }
        return $formula;
    }

    Notes about the Snippet:
    – You will need to change the “Form ID 6” to your actual form ID on the line numbers 18 & 32.

    – You will need to update the “calculation-1” field ID to the calculation field ID as per the calculation field ID on your form. (Lines 21 & 36)

    – You will need to update the ACF field ID “number_1” to your ACF field ID on lines 9, 19 & 37.

    Once you’ve updated the Snippet with the respective IDs, you can add it to the /wp-content/mu-plugins/ directory as a mu-plugin.

    Here is a guide on how you can add a mu-plugin: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    I would suggest you make a full backup of the site or test the snippet on the staging site just for additional safety.

    Hope this helps. Should you need any further help, please reply to this topic and we’ll be glad to assist you.

    Kind Regards,
    Saurabh

    Thread Starter acrspf

    (@acrspf)

    Thank you, I will give this a try and let you know how it works πŸ™‚

    Thread Starter acrspf

    (@acrspf)

    One quick question, is it possible to do multiple fields with this, or is a different instance needed for each one? Thanks πŸ™‚

    Hi @acrspf

    I pinged our SLS Team to review this once more and we will update you soon. Thank you for your patience while we look into this further.

    Kind Regards,
    Kris

    Thread Starter acrspf

    (@acrspf)


    I can’t get your code to work, changed the fields, no results

    Maybe this might help. see the screenshot below πŸ™‚

    The “total rent” field is taken from an ACF field with a shortcode (it’s an html field, html-5, pulls data from ACF field sturgis_rally_price__week) via code in the functions.php

    I need to put that in the calculation box, add the fee, security deposit, calculate the sales tax and get a total.

    Edit: I can’t get the image shared from google drive to show up here…

    • This reply was modified 2 years, 4 months ago by acrspf.
    • This reply was modified 2 years, 4 months ago by acrspf.
    • This reply was modified 2 years, 4 months ago by acrspf.
    Thread Starter acrspf

    (@acrspf)

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @acrspf

    I’m afraid I can’t content of that URL because it’s private. That’s most likely also the reason why you couldn’t share it here. Can you switch the link permission to “Everyone with a link can View” option?

    Also, the code that was shared sets the calculation value to be the value fetched from the custom field but the shortcode wouldn’t work anyway. I suppose there may have been some misunderstanding on our end here.

    So try this:

    1. Instead of putting your shortcode in calculation fiedl, use the shared code but remove all the calculations from that calculation field;

    At that point, if the code is correctly configured (the custom field name, calculation field ID and form ID) that calculation field should simply contain the value of your ACF field.

    2. Now add another calculation fiedl on the form and put all your custom calculation there, in that new field; and instead of shortcode use that first caluclation field there.

    Kind regards,
    Adam

    Thread Starter acrspf

    (@acrspf)

    Thread Starter acrspf

    (@acrspf)

    #1 The shortcode is not in the calculations fields, like I said it’s in an html field. Shortcodes only work in html fields.
    I don’t have ANY calculations in the calc field yet, there’s no point when I can’t get my initial ACF value in there.

    Checked the plugin code config several times, it’s correct, but no value shows up in the calc field, and the “form fields” dropdown in the calc field is empty.

    Why is the ability to just select any existing custom field not a built-in part of Forminator? That’s absolutely necessary for a lot of sites.

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @acrspf ,

    Would you mind sharing your form with us so we can check this issue closer? Please see here how to export it https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    When you have exported the form please share it with us using pastebin.com or a service like gdrive.

    We will test code and form to see why it’s not working and how to fix it.

    kind regards,
    Kasia

    Thread Starter acrspf

    (@acrspf)

    Ok here we go. Here’s the form link https://drive.google.com/file/d/1Jv5O7F-LVYd0v67RaarI5xPSwefqVS45/view?usp=drive_link


    and here’s the mu-plugin code – which causes a critical error in wordpress now

    (“[07-Feb-2024 22:32:30 UTC] PHP Parse error: syntax error, unexpected single-quoted string “, true );”, expecting “)” in /home/sturgis/public_html/wp-content/mu-plugins/calc_field_1_custom.php on line 14″)

    mu-plugin
    https://drive.google.com/file/d/1VvYdsHi4PaonRPps8LSHteqmfjGYst2i/view?usp=drive_link

    Is that everything?

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @acrspf,

    The error is more related to the code you have added, there is a typo in the code.

    The following lines where you use sturgis_rally_price__week ends with two single quotes, ie:

    var calc_val = '<?php echo get_post_meta( $post->ID, 'sturgis_rally_price__week'', true ); ?>';

    Could you please try this updated code without the typos and see how it behaves?

    <?php
    add_action( 'wp_footer', 'wpmudev_calc_field_post_meta', 9999 );
    function wpmudev_calc_field_post_meta() {
        global $post;
        if ( is_a( $post, 'WP_Post' ) && !has_shortcode( $post->post_content, 'forminator_form' ) ) {
            return;
        }
    
        $calc_val = get_post_meta( $post->ID, 'sturgis_rally_price__week', true );
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($){
            setTimeout(function() {
                $('.forminator-custom-form').trigger('after.load.forminator');
            },100);
    
            $(document).on('after.load.forminator', function( e, form_id ) {
                if ( e.target.id == 'forminator-module-1080' ) { // Please change 6 to your form's ID.
                    var calc_val = '<?php echo get_post_meta( $post->ID, 'sturgis_rally_price__week', true );?>';
                    setTimeout(function(){
                        $('#calculation-1 input').val(calc_val).trigger('change');
                    },1000);
                }
            });
        });
        </script>
        <?php
    }
    
    add_filter( 'forminator_field_calculation_calculable_value', 'wpmudev_calc_field_meta_val', 10, 3 );
    function wpmudev_calc_field_meta_val( $formula, $prepared_data, $field_settings ) {
        if ( $prepared_data['form_id'] != 1080 ) { // Please change the form ID.
            return $formula;
        }
    
        if ( $field_settings['element_id'] == 'calculation-1' ) {
            $formula = get_post_meta( forminator_get_post_data( 'ID' ), 'sturgis_rally_price__week', true );
        }
        return $formula;
    }

    Please do let us know how that goes.

    Kind Regards,

    Nithin

Viewing 15 replies - 1 through 15 (of 21 total)

The topic ‘Using data from ACF fields in Calculations’ is closed to new replies.