# [Resolved] Fields calculation

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 2 voices.

Last updated by Luo Yang 7 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#459475

Hello all,

Hello all,
I need the help/custom work/hint how to set post fields calculation. Egg. I have come custom fields group, and I need calculation of some fields from two fields groups.

Example:

Field group 1:

price 1+price2+price3*0,183=Total (also custom field)

Field group 2:

Total+price 4=Final price

The final price i need divide with another custom field (number of person)

Final price/persons=Price per person

Is it possible? If yes, I will be very happy ?

Thank you!!!!

I've posted similar thread in Toolset Professional Support, so look at on it (moderator answers)

https://toolset.com/forums/topic/post-fields-calculations/

#459684

Dear Renato,

https://toolset.com/forums/topic/calculator/#post-24976

If you needs more assistance for it, please duplicate same problem in a test site, and fill below private detail box with login details. also point out the problem page URL, and where I can edit your PHP codes, I need a live website to test and debug. thanks

#460008

#461003

Thanks for the details, I have modified below in your website:
1) Modify the post type "Yachts", enable the option "Editor", then we can debug the codes in a single "Yachts" post
2) modify the post:
setup a demo code in the content:

```Price per week 27 * Yacht commission <br />
= [types field="price-per-week-27"][/types] * [types field="yacht-commission"][/types] <br />
= [wpv-calculate] [types field="price-per-week-27"][/types] * [types field="yacht-commission"][/types] [/wpv-calculate]
```

3) Add below codes in the theme/functions.php:

```add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode(\$atts,\$content=null) {
\$content = do_shortcode(\$content);
\$content = eval("return \$content;");
return round(\$content, 2);
}
```

see the result:
It is only a demo, you will need to customize it as what you want.

#461037

Thank you Luo!

Do I need to put this code to every single item or is there a way to add one calculation for all items? Maybe in conditional display?
Question: Is there any way to include calculated result in further calculation?

Thanks so much!

Regards

#461059

Q1) Do I need to put this code to every single item or is there a way to add one calculation for all items? Maybe in conditional display?
It depends on your self, if you need to use same codes for all single item(post), I suggest you setup a content template for those single item(post), and put your custom codes into the content template, then all single item will be able to display it.
see our document:
Content Templates – WordPress Custom Page Template Without PHP
https://toolset.com/documentation/user-guides/view-templates/

Q2) Is there any way to include calculated result in further calculation?
Above shortcodes can only display the calculated result in the front-end, it can not store the result into database, and it is a new question, which is different from your original question:
https://toolset.com/forums/topic/fields-calculation/#post-459475
And it needs custom codes too.

If the original question is resolved, please create new thread for new question, this will help other users to find the answers. thanks

#461585