Skip Navigation

[Resolved] get the sum of custom fields after submit a form using submit api

This thread is resolved. Here is a description of the problem and solution.

Problem:
A form includes several numerical fields. The total should be calculated when the form is submitted and stored in another field.

Solution:
You will need to use the Forms API hook cred_save_data and write a snippet of custom code that retrieves the field values, performs the calculation, and stores the result.

An example of the kind of code required is given below: https://toolset.com/forums/topic/get-the-sum-of-custom-fields-after-submit-a-form-using-submit-api/#post-1168813

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by SteffenM1628 6 years ago.

Assisted by: Nigel.

Author
Posts
#1168782

Tell us what you are trying to do?

i wonder if thats possible with the submit api.
i have 4 fields and i want to calculate all fields together to get a sum and than save that sum in another custom field of this posttype.

field1+field2+field3+field4= save the total of all 4 fields in another custom field.

But i am no coder so it is very difficult for me to get this running.

Any ideas?

Thank you so much

#1168813

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Here is an example using the cred_save_data hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data).

You'll need to edit the slugs of the fields to sum, the slug of the field which will store the results, as well as the ID of the form.

Hopefully the code is clear enough to follow.

You can add it at Toolset > Settings > Custom Code.

/**
 * Custom form action to sum field values
 */
add_action( 'cred_save_data', 'tssupp_form_submit', 10, 3 );
function tssupp_form_submit( $post_id, $form_data ){

	if ( in_array( $form_data['id'], array( 123 ) ) ) { // Edit form ID

		$slugs = array( 'field1', 'field2', 'field3', 'field4' ); // Edit slugs of fields to SUM
		$result_slug = 'field5'; // Edit for slug of field to store result

		$values = [];

		foreach ($slugs as $key => $slug) {

			$values[$key] = get_post_meta( $post_id, 'wpcf-' . $slug, true );
		}

		$sum = array_sum( $values );

		add_post_meta( $post_id, 'wpcf-' . $result_slug, $sum );
	}
}
#1168819

hey nigel,

some question are left over
please check the code

<?php
/**
 * New custom code snippet.
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

/**
 * Custom form action to sum field values
 */
add_action( 'cred_save_data', 'tssupp_form_submit', 10, 3 );
function tssupp_form_submit( $post_id, $form_data ){
 
    if ( in_array( $form_data['id'], array( 985 ) ) ) { // Edit form ID
 
        $slugs = array( 'wpcf-skala-job-schwerpunkt-1', 'wpcf-skala-job-schwerpunkt-2', 'wpcf-skala-job-schwerpunkt-3', 'wpcf-skala-job-schwerpunkt-4' ); // Edit slugs of fields to SUM
        $result_slug = 'wpcf-skillswert-speichern'; // Edit for slug of field to store result
 
        $values = [];
 
        foreach ($slugs as $key => $slug) {
 
            $values[$key] = get_post_meta( $post_id, 'wpcf-' . $slug, true ); // What shell i add here: wpcf-
        }
 
        $sum = array_sum( $values );
 
        add_post_meta( $post_id, 'wpcf-' . $result_slug, $sum ); // What shell i add here: wpcf-
    }
} 

Another question is where does the calculating is going on because the sum of all fields needs ((sum *100)/40) to get the percentage of the sum. but i can´t see math anywhere?

#1168825

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You don't need the wpcf- prefixes for the fields because these get added where required in the code.

The maths happens with array_sum, which adds up the field values.

You can perform some further arithmetic to calculate $sum.

#1168854

hey nigel,

thanks for your advice. but the code does not save any value in the new field. the code runs fine but nothing happens.

#1168860

My issue is resolved now.
i have found the problem. As you said don´t need the prefix!
Thank you so much!