Skip Navigation

[Resolved] Calculate Custom Field

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 8.31 hours from now. Thank you for your understanding.

Tagged: 

This topic contains 13 replies, has 3 voices.

Last updated by MrHinsh 6 years, 2 months ago.

Assigned support staff: Waqas.

Author
Posts
#314206

I would like to do a calculation between 2 Custom Fields and save the result in another field:

Field A
Field B
Field C

Field C = Field B - Field A

I have created the short code for the calculation:

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts) {
   return wpv_condition($atts);
}

In my Forms I'm using the following:

[wpv-calculate evaluate=" [types field="field-b"][/types] - [types field="field-a"][/types]"]

To update Field-C, I have used the following trick


[cred_field field="field-c" post="opdrachten" value="[wpv-calculate evaluate=" [types field="field-b"][/types] + [types field="field-a"][/types]"]" urlparam=""]

This is giving me a parse error when Field-A and Field-B are still empty.

What is a solid solution to update a calculated Custom Field?

#314246

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

If this is supposed to update the Field C when CRED form is submitted, I will suggest to use CRED API for this purpose. For example:

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']==12)
    {
        if (isset($_POST['my_custom_field']))
        {
            // do some calculation here
            // ....

            // update custom field with the calculation
            update_post_meta($post_id, 'my_custom_field', $calculated_value);
        }
    }
}

You should adjust the above code around form ID, calculation logic and Field C. And add the code to your theme's functions.php file.

Also test for emptiness of the fields before applying a maths operation, otherwise you will always run into the parser errors.

Please see following resources for more information on CRED API and WordPress' update_post_meta():

- https://toolset.com/documentation/user-guides/cred-api/#csd
- https://codex.wordpress.org/Function_Reference/update_post_meta

Please let me know if I can help you with anything related.

#314582

Thanks - I have translated this to the following PHP function, that I have added to my function.php.

But the field is not updated.

// Update the GAS Usage in Form "Meterstanden"

add_action('cred_save_data', 'meterstanden',10,2);
function meterstanden($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==2606)
    {
        if (isset($_POST['wpcf-gasverbruik-1']))
        {
            // do some calculation here
            // ....
            $ms1voor = get_post_meta($post_id, 'wpcf-meterstand-1-voor', true);
            $ms1na = get_post_meta($post_id, 'wpcf-meterstand-1-na', true); 
            $verbruikms1 = $ms1na - $ms1voor;

            // update custom field with the calculation
            update_post_meta($post_id, 'wpcf-gasverbruik-1', $verbruikms1);
        }
    }
}

The calculation I need to do is the following:

wpcf-gasverbruik-1 = wpcf-meterstand-1-na - wpcf-meterstand-1-voor

#314697

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Your code looks perfect and should work fine. However, there might be some other reasons around this.

Can I ask for a temporary access to your site? So I can look for more details.

I have enabled your next reply as private, please input all details in that area. Please mention the links to the pages, views, forms, CPTs and configurations in question.

Please take a backup of your site, before proceeding.

#315104

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing the details. Please allow me some time to work on this. I will update you as soon as I find a solution.

#315319

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

WBT Electron BV.png

I am just trying to log into your website (please see attached), and have tried the password you supplied. But it doesn't seem to work. Please have a look.

I also can't see a user name field there, however, I tried username and password (one-by-one) for that field appearing in the screen shot.

Please let me know, thanks.

#315324

The website is also password protected - In my earlier private section you have received the admin login details that are still valid.

Please log in as admin to get access.

#315334

I tried also a solution to use the formula "[wpv-calculate evaluate=" [types field="field-b"][/types] - [types field="field-a"][/types]"]" as placeholder as part of the input fields in the form.

However, this is working when the fields that are used for the calculation do have a value. When you enter the details for the fist time, it seems to be the fact that these fields do not have a default value that is numeric.

Is there an option to get always a default "0" in these numeric fields. In that way there is also no need to have an additional function in the function.php.

Regarding to the function that has been defined in the function.php.
What is the meaning of "if (isset($_POST['wpcf-gasverbruik-1']))"? The field has still no value until this function has been completed.

#315413

It is working now - I saw that there was a small mistake in the field definitions.

Instead of "wpcf-meterstanden-1-xxx" , I just entered "wpcf-meterstand-1-na". So than it won't work.

I have created the three formulas now in a sequence:

add_action('cred_save_data', 'meterstanden',10,2);
function meterstanden($post_id, $form_data)
{
    // if a specific form
    if (($form_data['id']==2606) || ($form_data['id']==2602)) 
  
    {
        if (isset($_POST['wpcf-gasverbruik-1']))
        {
            // do some calculation here
            // ....
            $ms1voor = get_post_meta($post_id, 'wpcf-meterstanden-1-voor', true);
            $ms1na = get_post_meta($post_id, 'wpcf-meterstanden-1-na', true); 
            $verbruikms1 = $ms1na - $ms1voor;
	
        // update custom field with the calculation
            update_post_meta($post_id, 'wpcf-gasverbruik-1', $verbruikms1);
        }
        if (isset($_POST['wpcf-gasverbruik-2']))
        {
            // do some calculation here
            // ....
            $ms2voor = get_post_meta($post_id, 'wpcf-meterstanden-2-voor', true);
            $ms2na = get_post_meta($post_id, 'wpcf-meterstanden-2-na', true); 
            $verbruikms2 = $ms2na - $ms2voor;
	
        // update custom field with the calculation
            update_post_meta($post_id, 'wpcf-gasverbruik-2', $verbruikms2);
        }
        if (isset($_POST['wpcf-gasverbruik-totaal']))
        {
            // do some calculation here
            // ....
            $mst1 = get_post_meta($post_id, 'wpcf-gasverbruik-1', true);
            $mst2 = get_post_meta($post_id, 'wpcf-gasverbruik-2', true); 
            $verbruiktotaal = $mst1 + $mst2;
	
        // update custom field with the calculation
            update_post_meta($post_id, 'wpcf-gasverbruik-totaal', $verbruiktotaal);
        }
    }
}

Is this the right way to update the 3 fields or can this be done in a smarter/faster way?

#315427

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your feedback. Please consider followings:

A) I am trying to use that WP-Admin access details you provided in the private message. I can only see a password field, that doesn't accept the supplied password.

B) Yes that's the correct way of writing the code. Since calculations are required based on different fields and in different ways, you have coded it pretty well.

Please let me know if the issue is resolved, so there's no need to further investigate the matter (and the access details). Or, please let me know if I can help you with anything related.

#315666

What is the meaning of

if (isset($_POST['wpcf-gasverbruik-totaal']))

?

For me it is not clear when this If statement is true.
Does the mentioned field to be on the form?
Or is the "islet" related to a certain value?

Could you explain this to me?

#315678

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Yes, the code reads as below:

- if $_POST['wpcf-gasverbruik-totaal'] was submitted by a form and is present.
- $_POST is a global array, which holds all form data (sent via POST method).
- Field 'wpcf-gasverbruik-totaal' should be on the form, so that the 'if' condition is met and becomes true.

#315969

Thanks - this will help to reuse this knowledge also for other solutions

#436948

Guys,

I am trying to wrap this into a WordPress Plugin so that it does not break when my theme updates... and I am having a bit of trouble as my PHP is completely non existent.

hidden link

I keep getting an error on line 16: Parse error: syntax error, unexpected 'function' (T_FUNCTION) in /nas/content/staging/nakedalmweb/wp-content/plugins/wp-calculate-rate-1/wp-calculate-rate.php

It looks fine to me... Any advice would be much appreciated as I am lost.

-MrHinsh