Home › Forums › Backend Issues (wp-admin) › How to create calculated fields
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
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.
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,
You must be logged in to reply to this topic.
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.