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']);
});
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: hidden link
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 ).
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']);
});
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.