# [Gelöst] Math with custom fields values

### Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
How to perform simple calculations with custom field values to output, for example "Total = " field1 + field2.

Solution:
This would require registering a custom shortcode to use which first parses the Types shortcodes to get the values then evaluates the expression and returns the result.

Here is such a shortcode:

/** * Add custom shortcode to perform calculations with Types fields * * attribute 'places' for decimal places, defaults to zero */ add_shortcode('calculate', function( \$atts = [], \$content=null ) { \$atts = shortcode_atts([ 'round' => 0 ], \$atts ); \$content = wpv_do_shortcode(\$content); \$content = eval("return \$content;"); return round(\$content, \$atts['round']); });

An example of the shortcode in use:

[calculate round=2][types field="weight"]*[types field="unit-price"]/100[/calculate]

Relevant Documentation:
https://toolset.com/toolset-support-policy/

100% of people find this useful.

This support ticket is created vor 7 Jahre. There's a good chance that you are reading advice that it now obsolete.
Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 Antworten, has 2 Stimmen.

Last updated by diyanK vor 7 Jahre.

Assisted by: Nigel.

Author
Artikel
#509251

I am reposting this because my original thread got closed.
https://toolset.com/forums/topic/help-me-do-a-little-math-with-custom-fields-values/

I have a series of custom fields that hold numerical data.

I would like to make some calculations based on that data and show it on the front end.
I am using [types] shortcodes to output the data from my fields.

For example:
I have custom field 1 that holds the value: 10 (USD)
and custom field 2 that holds the value: 3 (KG)

I would like to do the math and show the price per kg (which would be cf1 divided by cf2 ) on the front end.

More details:

Please take a look at the following page:

This is a product page. I have specified a price of 150BGN for a package of 25kgs of honey.

When I add the product I have the option to specify the quantity in weight, volume or pcs.
( I did this with using a CRED form with conditionals, user first picks whether he'll be adding packs of weight, volume or quantity(pcs). When the user picks one then the option of adding the values and prices come up, based on what he selected.

In this case we have defined a pack that weights 25kgs and a price of 150BGN.

I would like to get an automated calculation that would show the price per kilo. ( 150 divided by 25 )

Also I would like to be able to do the math for the volume ( price per liter ) in cases when we add a pack of 50ml with a price of 7BGN (or another case - 1500ml with a price of 300BGN). I would like to get a price per 1000ml calculated automatically.

Same goes for the packs of quantity in pcs - in case we have a pack of 25pcs priced at 300BGN get a calculation that would show the price per 1piece ( 300 divided by 25 ).

Thanks a lot!

#509325

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

Hi there

To perform calculations with values from Types fields you will need to register a custom shortcode which first parses the Types shortcodes to get the values then evaluates the expression and returns the result.

You can register the shortcode 'calculate':

```/**
* Add custom shortcode to perform calculations with Types fields
*
* attribute 'places' for decimal places, defaults to zero
*/
add_shortcode('calculate', function( \$atts = [], \$content=null ) {

\$atts = shortcode_atts([ 'round' => 0 ], \$atts );

\$content = wpv_do_shortcode(\$content);
\$content = eval("return \$content;");
return round(\$content, \$atts['round']);
});
```

You would use it like so:

```[calculate round=2][types field="weight"]*[types field="unit-price"]/100[/calculate]
```

I didn't test that, so please let me know how you get on with it.

#509334

Hi Nigel,

Thank you for looking into this.

Sorry for the dumb question, but where do paste this register code?
Obviously it's not this Dahsboard-> Toolset-> Settings-> Front-end Content ..

Perhaps functions.php ?

#509338

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

Yes, in your theme's functions.php, or you can use a plugin such as Code Snippets to add the code (which won't be lost if you change theme).

#509345

Hmm, WP shouts a syntax error when I try to add this to my functions.php

#509346

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

Sorry, I see a simple typo missing the closing parenthesis.

I edited my reply above to include it, if you want to try again.

#509348

OK so far so good, now we need to figure out what's the right formulae.

The current setup returns 37.5

My code looks like this:

``` Цена за 1кг: <span style="text-decoration: line-through;">[calculate round=2][types field="product-weight"]*[types field="product-price"]/100[/calculate]</span>лв
```

Where the values are as follows:
product-weight = 25 (kgs)
product-price = 150 (BGN)

See where it says: Цена за 1кг: 37.5лв :

#509378

Nigel
Supporter

Languages: Englisch (English ) Spanisch (Español )

Timezone: Europe/London (GMT+01:00)

If you have a price and a weight in kilos, then the price per kilo is price/weight.

```[calculate round=2][types field="product-price"]/[types field="product-weight"][/calculate]
```

If you are going to add a number of different calculations you'll need to work the math out.

#509380

Thanks, this worked.

I have no idea what I did wrong when I tried to figure out the formulae on my own, huh I know this is simple math.

Thanks for your input, this issue has been resolved, I will figure out the rest on my own, using conditionals that would trigger different formulaes based on the type of measures.

Thanks a billion again!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.