advancedforms / advanced-forms

WordPress plugin to create forms using Advanced Custom Fields
75 stars 14 forks source link

Calculated Fields dont work in acf group #64

Open MPHood opened 5 years ago

MPHood commented 5 years ago

Calculated fields dont work if the fields arr inside an acf group.

Is there any workaround?

fabianlindfors commented 5 years ago

Hi!

Do you mean that using {field:field_name[sub_field_name]} doesn't work with calculated fields?

MPHood commented 5 years ago

Thats whats happening:

  1. I create a calculated field inside an acf group that i assigned to a form
  2. i go to the form click on the calculated field tab and its showing: there are no calculated fields assigned to this form. Add a calculated field with ACF and then come back here to configure its contents!

If I place a calculated field outside the group, it works.

What I´m trying to do:

function ersparnis_berechnen3() {

// I WANT TO GET THE VALUES OF THE (SUB)FIELDS INSIDE THE ACF GROUP - DO I NEED to use something else than af_get_field in order to get the field values that are inside an ACF Group?

$val1 = af_get_field( 'verbrauch11]' ); 
$val2 = af_get_field( 'verbrauch_Kopie1' );
$val3 = af_get_field( 'verbrauch31' );
$val4 = af_get_field( 'verbrauch41' );

// Calculate the sum of the field values

$verbrauchb = $val1 + $val2 + $val3 + $val4;

// Calculate the sum of the current field values

$sparb = $verbrauchb * 0.09;

// Display the sum in the calculated field

return ' Bis zu ' . $sparb . ' Euro/Jahr! '; }

add_filter( 'af/field/calculate_value/name=spar2', 'ersparnis_berechnen3', 10, 0 );

The Code works if the Calculated filed is outside the ACF group.

I hope you can understand my problem.

Greets

fabianlindfors commented 5 years ago

I think I understand now!

The UI currently won't work if you place a calculated field inside a group. What should work though is the af/field/calculate_value filter you're currently using.

If you want to use af_get_field to get values of fields inside groups you'll need to fetch the full group using af_get_field( GROUP_FIELD_NAME ). You will then get an array containing the values of all subfields.