Skip Navigation

[Resolved] How to calculate two custom fields and output to another custom field?

This support ticket is created 4 years, 4 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 19 replies, has 2 voices.

Last updated by StanleyT8485 4 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#1754205

Tell us what you are trying to do?
I want to calculate the values of two custom fields (numeric) and output to another custom field.

#1756171

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please tell me where exactly you want to make this calculation.

On the admin site while editing post or using the Toolset form?

#1757179

Hey Minesh,

On the admin. The custom fields are attached to a certain post type.

Stan

#1757331

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So, why dont you use the standard WordPress hook save_post and grab the custom field values and make addition of that and store it to your third custom field.

More info - please check the following example and adjust your code accordingly.
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/#an-example

#1757339

Do I have to manually save the post to perform the calculation for the custom field?

There will be over hundreds of posts that require the calculation every 30 mins. Is there any way we can automate this?

Stan

#1757361

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no such feature. As you know, that would obviously require custom code and for that you will require to write that on your own.

#1757369

How about not putting the calculation into a custom field?

The main goal is to show the calculation in a View.

#1757373

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - but what field you want to display within the view?

Normally, whatever field you want to display within the view you will just require to add it within the view's Loop Editor section and it will be get displayed on frontend where you will add your view.

#1757419

I want to show the calculation of two custom fields.

Can I perform calculations in the editor like [custom field 1] + [custom field 2]? If I can, that would be amazing!

#1757437

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You will require to write the custom shortcode that do the calculation and return the results.

#1757439

Thanks. Is there any documentation I can follow to implement this?

#1757441

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Here is the related ticket with the solution you are looking for - can you please check if that helps?
=> https://toolset.com/forums/topic/calculating-types-custom-fields-and-display-in-html-container/#post-345125

#1757545

Thank you! I got it with the code you provided in the link.

add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
return $content;
}

Another question. This might be a bit more complicated.

One of the custom fields I want to calculate is from a Child post. However, Toolset doesn't allow me to show a custom field from a Child post in the current post. It needs a View to show this custom field.

Screenshot of the custom fields I want to calculate: hidden link

I tried putting the View in the [wpv-calculate] shortcode and it resulted in a syntax error.
[wpv-calculate][types field='ticker-price' output='raw'][/types]+[wpv-view name="test-price-to-book-fy20"][/wpv-calculate]

Is there a workaround this?

#1757625

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to review your view and where you added the calculation shortcode.

Can you please share problem URL and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1757697

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I would like to know to what column you want to display the calculated amount? with "WALE" column or "Occupancy" column?