Skip Navigation

[Resolved] Custom Fields Calculations

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

Problem: I have a Form where Users are asked to check off several sets of checkbox custom fields. I would like to calculate how many of each type of checkbox was checked, and display the total number of checked checkboxes of each type in the post template.

Solution: Use the cred_save_data API and the Types Field API to determine which checkboxes were checked and add up the totals for each type of checkbox. Save those totals in 4 new custom fields, and display the values of those custom fields in the post template.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/customizing-sites-using-php/functions/

This support ticket is created 3 years, 5 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#2114403
diyhouseinspection.com_web_property-listing_123-test-avenue-hereford-texas-1150_.png

Hello,

We have a posts form that will be used to home inspect your own home. The customer has requested that after selecting the appropriate check box a score be calculated at the bottom of the post after it has been submitted.

Is this functionality possible? Please advise. Below are the steps to get to the page with the calculations and a "mockup" of how the form would look with calculations.

FYI:

• Custom Fields Post Field Group: DIY Structural Systems Group
• Post Forms: DIY Structural Systems Form

Step 1. Structural Systems Form is filled out.
hidden link

Step 2. Results with Calculations for each category on the bottom of the page.

hidden link

Please let me know where I can submit login credentials confidentiality

#2114715

Hello, there's no built-in summation system like this for custom fields submitted in Forms, but we provide some PHP APIs that can be used to achieve something similar to what you've described here. You could inspect the values submitted in the Form and use those to calculate the totals, then save those values into some other custom fields created to store the totals.

Our Forms API cred_save_data can be used to trigger custom PHP code upon Form submission. We have documentation for this API available here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

You can use the WordPress function get_post_data to get the values of each custom field submission:
https://developer.wordpress.org/reference/functions/get_post_metas

I would need to check the fields and see how they are set up to give you some additional code examples you could use to create a custom solution. I have activated private reply fields here so you can share login credentials securely.

#2115365

Okay I have set up a similar test scenario on my local site and created some example code for you to use as a template. Here are the instructions for implementation:
1. Add 4 "number" custom fields in the DIY Structural Systems Group or in another separate Field Group applied to the Property Listings post type. Each of the custom fields will store one of the 4 calculated values. The slugs should be something like:
- total-inspected
- total-uninspected
- total-ok
- total-deficient

2. Add and adjust this cred_save_data hook in your child theme's functions.php file, or in a new snippet in Toolset > Settings > Custom Code:

// calculate number of inspected, uninspected, ok and deficient checkboxes
// https://toolset.com/forums/topic/custom-fields-calculations/
add_action('cred_save_data', 'calculate_diy_field_totals',10,2);
function calculate_diy_field_totals($post_id, $form_data) {
  $forms = array( 331 );
  $total_inspected = 0;
  $total_uninspected = 0;
  $total_ok = 0;
  $total_deficient = 0;
  if ( in_array( $form_data['id'], $forms ) )
  {
    // calculate and store total inspected
    if(types_render_field("grading-and-drainage", array("item" => $post_id, "option" => 0)) == 'Inspected 1') {
      $total_inspected+=1;
    }
    if(types_render_field("roof-covering", array("item" => $post_id, "option" => 0)) == 'Inspected') {
      $total_inspected+=1;
    }
    // copy and paste additional "if" blocks here for the remaining "inspected" checkboxes
    update_post_meta( $post_id, 'wpcf-total-inspected', $total_inspected );

    
    // calculate and store total uninspected
    if(types_render_field("grading-and-drainage", array("item" => $post_id, "option" => 1)) == 'Not Inspected 1') {
      $total_uninspected+=1;
    }
    if(types_render_field("roof-covering", array("item" => $post_id, "option" => 1)) == 'Not Inspected') {
      $total_uninspected+=1;
    }
    // copy and paste additional "if" blocks here for the remaining "uninspected" checkboxes
    update_post_meta( $post_id, 'wpcf-total-uninspected', $total_uninspected );
    

    // calculate and store total ok
    if(types_render_field("if-inspected-grading-drainage", array("item" => $post_id, "option" => 0)) == 'OK') {
      $total_ok+=1;
    }
    if(types_render_field("if-inspected-roof-covering", array("item" => $post_id, "option" => 0)) == 'OK') {
      $total_ok+=1;
    }
    // copy and paste additional "if" blocks here for the remaining "ok" checkboxes
    update_post_meta( $post_id, 'wpcf-total-ok', $total_ok );
    

    // calculate and store total deficient
    if(types_render_field("if-inspected-grading-and-drainage", array("item" => $post_id, "option" => 1)) == 'Deficient') {
      $total_deficient+=1;
    }
    if(types_render_field("if-inspected-roof-covering", array("item" => $post_id, "option" => 1)) == 'Deficient') {
      $total_deficient+=1;
    }
    // copy and paste additional "if" blocks here for the remaining "deficient" checkboxes
    update_post_meta( $post_id, 'wpcf-total-deficient', $total_deficient );
  }
}

I set up code for evaluating the Grading and Drainage group and the Roof Covering group so you can see how this might work. You will need to add code for the remaining checkbox groups, everywhere I left comments like "copy and paste additional 'if' blocks here..."

Each time you copy + paste an "if" block, you should adjust the field slug and the text displayed when each checkbox is checked. You can find those pieces of information in the Field Group editor. Notice that the text displayed is not always consistent - for example in Grading and Drainage it is "Inspected 1" if inspected, but in Roof Covering it is "Inspected" if inspected. So you probably need to use the Field Group editor page as a reference when updating the code.

When the Form is submitted, if the code is added correctly the new custom fields will be updated with the correct totals. If you use different field slugs for your new fields, you need to update the update_post_meta functions to point to the correct field slugs. You can update the Content Template applied to the Property Listings posts to display these new custom field values in the correct areas.

Let me know if you run into problems and I can take a closer look.

#2116635

Hey Christian, Happy Friday! You are a genius, sir! That works great! Your help is so appreciated. THANK YOU!

On a separate subject, our client wants this form to be emailed as well and a Print Button on the listing page after the form is submitted, do you have any recommendations?

#2118057

Hi, our support policy is to address one issue per ticket so I have some general information here. If you'd like to discuss in more detail, I can create separate tickets for you.

On a separate subject, our client wants this form to be emailed as well
Toolset Forms offers an automatic email notification feature you can use to trigger automatic emails upon Form submission. I am including a link to some documentation for you to review. Let me know if you'd like to discuss this process in more detail, and I can split off a new ticket.
https://toolset.com/course-lesson/send-notifications-when-someone-submits-the-form/

... and a Print Button on the listing page after the form is submitted
Toolset does not provide a built-in Print button feature, but you could create such a print button or print link using HTML, and insert it in your Content Template using a Custom HTML block. It would be displayed on every post where the Content Template is applied. For example, see the following links with example print dialog-triggering buttons or text links:
https://wcmshelp.ucsc.edu/advanced/print-button.html
https://css-tricks.com/quick-tip-making-a-print-this-page-button/
https://www.youtube.com/watch?v=iS238z-xqqQ

Print-friendly stylesheets:
https://www.sitepoint.com/css-printer-friendly-pages/
https://css-tricks.com/css-tricks-finally-gets-a-print-stylesheet/
https://alistapart.com/article/goingtoprint/

Again, I can split off a new ticket if you'd like to discuss in more detail.

#2119427

Hello Christian, I completely understand your policy. Thank you for sharing the links, they were helpful.

Our totals seem to be off on "Ok" and "Deficient" do you mind taking a look?

#2119985
Screen Shot 2021-07-20 at 9.57.12 AM.png
Screen Shot 2021-07-20 at 9.39.56 AM.png

Our totals seem to be off on "Ok" and "Deficient" do you mind taking a look?
Sure, I checked the code and noticed a couple of things that look suspicious. In one spot it looks like the $total_ok variable was not updated after copying + pasting code from the $total_uninspected calculation section, and in another spot it looks like the If Inspected Grading and Drainage custom field slug is inaccurate. Screenshots attached here. Please make those changes and let me know the results.

#2120153

My issue is resolved now. Thank you for all your help Christian! You are a life saver.

#2122831
Screen Shot 2021-07-22 at 7.27.26 PM.png

Hi Christian, our backup restore wiped out our custom code, so we had to rebuild it. We seem to be getting an error, do you mind taking a look and seeing where we might of gone wrong? I have attached a screenshot of the error message.

Thanks in advance.

#2123397

Good morning Christian, please disregard. We were able to dig up the custom code file and it is fully functional again.