Skip Navigation

[Resolved] Sum of custom fields selected

This support ticket is created 2 years, 11 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by adamG-6 2 years, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2275467

Tell us what you are trying to do?

I have up to 6 scoring questions each with a possible score of up to 10 - chosen by select field. I would like to add these scores to store a total score.

Is there a similar example that we can see?

I have tried using this but it just returns 0

function calculate_fields( $post_ID ) {

if ( get_post_type( $post_ID ) == 'application1' ) {

$totalscore = $score1 + $score2 + $score3 + $score4 + $score5 + $score6;
$score1 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-1',true);
$score2 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-2',true);
$score3 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-3',true);
$score4 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-4',true);
$score5 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-5',true);
$score6 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-6',true);

update_post_meta( $post_ID, 'wpcf-cja-longlist-score', $totalscore );

}

}
add_action( 'save_post', 'calculate_fields', 99 );

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
// if a specific form
if ($form_data['id']==561) {
calculate_fields( $post_id );
}

#2275945

Hi,

Thank you for contacting us and I'd be happy to assist.

Looking into the code, the line that is calculating the total score should be placed after the field values have been stored in their variables:


$score1 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-1',true);
$score2 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-2',true);
$score3 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-3',true);
$score4 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-4',true);
$score5 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-5',true);
$score6 = get_post_meta($post_ID, 'wpcf-assessment-criteria-score-6',true);

$totalscore = $score1 + $score2 + $score3 + $score4 + $score5 + $score6;

This should do the trick.

regards,
Waqar

#2276063

My issue is resolved now. Thank you!