Support

Account

Home Forums Backend Issues (wp-admin) How to create calculated fields

Helping

How to create calculated fields

  • Hi,

    I am trying to create several fields that are calculated automatically using values from ACF fields. I am using ACF Pro and Elementor Pro. I also use Code Snippets plugin in order to create snippets.

    Found similar question on this forum
    https://support.advancedcustomfields.com/forums/topic/how-to-create-a-calculated-acf-field-saved-in-the-db/

    So I have this values, that I input manually:
    _price – just a woocommerce price
    area – total area of the apartment in sqm
    guarantee_return – number that shows the % of yearly income
    return_term – for how many years buyer gets the income

    Values that must be calculated automatically:
    total_percent = “guarantee_return” * “return_term”
    total_return = “guarantee_return” * “return_term” * “_price” / 100
    Price_per_sqm = “_price” / “area”

    I already created hidden ACF fields (as in link above) to save this data in DB.

    My first snippet works perfectly (screenshot 1). The value shows up on the page after updating the product. But when I create second snippet (Screenshot 2), value doesn’t appear.
    I am not good at coding, but I am sure that there is no problem in calculations because when I use formula from the second snippet in the first snippet it works fine. I think that is coding issue.

    So now I need 3 fields to calculate, later I will need more. May be there is the way to place them all in one snippet?

    Looking forward to your help.

    Kind regards,
    Paul

  • Hi there,

    Your code seems good.
    As you said, if you’re calculating your “total_return” into first snippet only, your calculation is working.

    Why don’t you update all your field in a single snippet (your first one) ?

    If you really need to use different snippets for each field, maybe you could add different priorities to your actions.
    See documentation: https://www.advancedcustomfields.com/resources/acf-save_post/

    So keep your first snippet without modifications (default priority is 10).
    And try adding a priority of 15 for your second snippet :
    add_action('save/post' 'my_acf_update_total_return', 15);

    Let me know if it helps,

    RemSEO

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

The topic ‘How to create calculated fields’ is closed to new replies.