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']==1337) {
calculate_fields( $post_id );
}
}
100% of people find this useful.
This support ticket is created 7 years, 2 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.
Our next available supporter will start replying to tickets in about 7.18 hours from now. Thank you for your understanding.
I created a custom post with custom fields in it all are number type
I have 3 fields that I want to calculate and store in the custom post. if the other numbers are changing the calculated fields will be updated automatically.
Total assets = sum of all assets
Total liabilities = Sum of all liabilities
Net Worth = Total assets - Total liabilities
each asset or liability is entered manually when saving/editing/updating the post type numbers I want the 3 calculations to be updated and insert the new numbers into the respective custom field
I have added the fallowing to my function file
// to allow for adding up totals in fields
global $total;
add_shortcode('add-to-total', 'add_total_shortcode');
function add_total_shortcode($atts, $content = '') {
global $total;
$string = str_replace('$', '', $string);
$total += wpv_do_shortcode($content);
}
add_shortcode('show-total', 'show_total_shortcode');
function show_total_shortcode() {
global $total;
$totalNew = $total;
return $totalNew;
}
// to allow math calculations on multiple fields
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
return wpv_condition($atts);
}
Thank you for contacting Toolset support. Please confirm the following points:
1. You have 3 custom fields, want to calculate these 3 fields and then store the result in 4th field?
2. If any of the 3 custom fields update, then result field automatically update?
To achieve this I need to request you to please provide temporary access (WP-Admin and FTP Login info) to your site (preferably staging site), so that I can look into your setup and check the issue.
Your next answer will be private which means only you and I have access to it.
=== Please backup your database and website ===
✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.
✙ Please add the links where these fields are created
Sorry we ran into the weekend. The login info you have provided is not working, actually its asking for verification code when I try to login and I am not sure which email it sends the verification - see attached screenshot.
Thanks, login works now, I have taken a look at your setup and fields. I need some clarifications:
- Total assets = sum of all assets
- Total liabilities = Sum of all liabilities
- Net Worth = Total assets - Total liabilities
I can see following fields please let me know which one are assets and which one are liabilities:
>> cash on hand, Checking accounts, Money market accounts, Credit cards, Home mortgages
And you want to calculate and update following fields whenever above fields changed:
>> Total Liabilities, Total Assets, Net Worth
Assets - cash on hand, Checking accounts, Money market accounts i.e. Total Assets = cash on hand = Checking accounts = Money market accounts
Liabilities - Credit cards, Home mortgages I.e. Total Liabilities = Credit cards + Home mortgages
Net Worth = Total Assets - Total Liabilities
Yes, auto calculate when any change of asset or liability field - Total Liabilities, Total Assets, Net Worth
please note that this is the above is a simple version of the calculation I want to have. in reality I will have a long list of assets and a long list of liabilities.
not sure if there is a better way to identify a custom field as an asset or liability and then just sum them programmatically
for instance create only 2 custom fields one is 'Asset' and the other is 'Liability' how ever each one 'Allow multiple-instances of this field' and each instance have a description ,of the asset or the liability, as well as the numeric value.
Thanks Noman,
it seems that things are working ok however I need to know where did you put the PHP code?
I would like to update the function in it to reflect other custom fields I'm going to add and need to know where it location.
I normally use a function plugin to create a separate function.php page so I dont lode anything with updates...
BTW I updated the theme to the latest version and I lost the calculations. I guess the above code was put under the theme function.php page.
I try to put the above code under my custom function.php page outside of the theme (you can find it here hidden link) but when I save it I get an error:
"This plugin has been deactivated because your changes resulted in a fatal error.
Parse error: syntax error, unexpected ' if' (T_STRING) in /var/www/vhosts/davidzohar.com/httpdocs/wp-content/plugins/functions.php on line 45"
at the moment I lost the calculation.
this is partially work.
allow me to explain.
I created a CRED form for the Net Worth Post type.
when I insert numbers into the form and submit it the initial calculation is not preformed. hence I get '0' for all calculated fields.
my expectation is that when I submit the from and its published automatically the fields will be calculated.
the behavior is different if I create the a new Net Worth Post by using the admin dashboard and creating the new Net Worth post and click the publish button.
my expectation is that if a user create or update the data using a cred form the calculated fields will be updated.
to create a new net worth I use the page (with the cred from in it) hidden link
the form is available here hidden link
> When I insert numbers into the CRED add new post form and submit it the initial calculation is not performed and I got '0' for all calculated fields.
We need to add cred hook to call the calculation function. I have added the following code in custom functions.php file, and it is working as expected, see attached screenshot. Please check here: hidden link
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']==1337) {
calculate_fields( $post_id );
}
}
I have one last question around this topic and its related to the last code you provided:
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']==1337) {
calculate_fields( $post_id );
}
}
how is this code need to look like if I have a few custom post type that I need to calculate every time they are update? I see the line : 'if ($form_data['id']==1337)' as only applying the calculation to the specific custom post type.
what happen if I have a few forms that need calculation on using CRED form (new or edit)? how can I change the code to be with an 'OR' statement rather than "=="?
==> In above code 1338, 1339, 1340 will be your new/edit cred form ids.
==> Please make sure to use one same Custom Field and assign it to your both CPTs. If new CPTs will have different Custom Field names then you need to update main function / code as well.