How To Calculate ACF Fields

Hi,

I would like to know how to calculate ACF fields.
So for example acf_a + acf_b = result.

I know that I can get the value of an ACF field with {acffield=my_field}.
Normally I calculate fields in a form, but here I don’t need any input.

So which method should I use to calculate two acf fields: php, java?
Or is there an inbuild method from Cwicly that I missed?

Best regards.

A plugin exists: Calculated fields for ACF.
I’m not sure if it still works with the new ACF versions.

1 Like

Hi @weedor,

thank you for the reply. I usually don’t like to install extra plugins, especially some with late update cycles and try to write a snippet myself.

Saw your post Calculated fields for ACF and will check it out.

1 Like

Since the additional plugin Calculated fields for ACF is not flexible enough + outdated! I made my own little snippet.

Worked with php as usual:

This snippet uses an ACF field from my post type products and adds a certain fee in %. Additionally a second fee can be added to the total price.

add_action('acf/save_post', 'change_price_from_value');
function change_price_from_value( $post_id ) {
	
	if (get_post_type($post) == 'products'){
		
		$price = get_field( "product_price", $post_id ); // price
		$charge = get_field( "options_charge", 'option' ); // % fee for all products
		$charge_plus = get_field( "product_charge_plus", $post_id ); // additional % fee
		$total = $price * ( 1 + $charge / 100 ) * ( 1 + $charge_plus / 100 );
		
		update_field('new_price', $total);
	}
}

Edit:
In the options Page I set a value of 10 % (fee). The New price field was set to read only.
From left to right: Price | Additional fee % | New price

Would like to know a preferred solution, or confirmation with my solution :thinking:

All thoughts and ideas are welcome.