Hello. I've seen some older tickets about registering a shortcode and performing simple calculations, but I haven't found anything about working with checkboxes (numeric values stored). What I have is:
Checkboxes A
-- Option 1
-- Option 2
-- Option 3
Checkboxes B
-- Option 1
-- Option 2
-- Option 3
I would like to add all the checked values from both fields and get a total. Depending on that total, I would then like to display a new value, i.e. if total is < 50, then display "Not passing score". If >=50, then "Passing score". Ideally I would like the final score to be stored in a separate custom field upon submitting/updating the form. I tried creating an Integer field and see if I can set some conditions, but fields of types Checkboxes don't appear to be available (because they are arrays, I assume).
Hello. Thank you for contacting the Toolset support.
Can you please share more details that you want to calculate the option values when user submit the form while add/edit post from backend or you are using a frontend post form. If you are using post form to what post type its attached to and please share how you configure the checkboxes at Toolset => Custom fields.
Please share all this required information, once you share that I will review that and then I'll be able to guide you in the right direction.
There is no such feature available and it will require custom code.
There are two ways, either you can use Javascript or you can use WordPress hook save_post (please note that save_post hook is not working as expected with Block editor in your case).
jQuery(document).ready(function($){
jQuery('input[name^="wpcf[count-options]"]').on("change",function(){
var sum = 0;
jQuery('input[name^="wpcf[count-options]"]').each(function(){
if(this.checked) {
sum = sum + parseFloat(this.value);
}
});
jQuery('input[name^="wpcf[total-field]"]').val(sum);
});
});
- You can change the custom field slug "count-options" and "total-field".
You can adjust the code for your multiple checkboxes field.