Home › Forums › General Issues › Make calculations
Hi i want to know how i can make calculations between fields.
Example:
Field1 – Width
Field2 – Height
Field3 – Price
Field4 – Total
Field1 x Field2 / Field3 = Field4
do it just like you would any other variables..
$height = floatval(get_field('height'));
$width = floatval(get_field('width'));
$price = floatval(get_field('price'));
$total = floatval(get_field('total')); //not neccessery, gets calculated.
$total = ($width * $height) / $price;
echo $total;
Or in a function in functions.php.. You can even put them in a function inside a custom plugin if you want.
All that’s needed is that they or the call to a function in which they are calculated are placed before the echo $total.
Easiest is indeed to put it in a page template where you want it to be shown.
Okay I understand, that’s great. But this function can only be called from a template, right? That can’t be done from the ACF Custom Fields menu in the wp backend, can it? Thanks for your reply in advance.
It can be called easily from a template.
You could probably create an ajax function to call this calculation in the backend using the many hooks available for ACF (check the documentation) but it’s not the easiest solution and if you’re not sufficiently good at php/ajax/wp hooks it will be quite the challenge.
The topic ‘Make calculations’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.