Home Forums General Issues Make calculations


Make calculations

  • Hi i want to know how i can make calculations between fields.


    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;
  • This code has to be inserted in a page template then, right?

  • 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.

  • This reply has been marked as private.
  • Hi,
    how to have TOTAL field into backend?

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Make calculations’ is closed to new replies.