Home › Forums › Front-end Issues › Only the last condition outputs as data-condition
I have created an option form with a repeater field (including date and session field) to accomplish the following:
Customers can make a booking via a booking form on frontend.
In the option page I can EXECLUDE dates and sessions via the repeater field as mentioned above.
In technical terms it should work like this:
if REPEATER DATE FIELD != frontend datepicker output && REPEATER SESSION FIELD != frontend session field (select) output >> show time field
The code below works accept for 1 thing: Always only the last entry of the repeater field is used in the data-conditions, all others are ignored:
(data-conditions="[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200627"}]")
I know this has to do with the foreach but man I have tried everything but it just won’t work with all entries.
Here’s the entire code:
// Apply conditions to fields
add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
function yl_check_booking_exeptions_session_1( $field ) {
// Retrieve option values. Date value should be like: 20200611 (unformatted)
if( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
while ( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
the_row();
$option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
$date = date_i18n('Ymd', strtotime($option_date));
$session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
if ( $session == '1' ) {
// Add the condition to the field
$field['conditional_logic'] = array(
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '==', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
);
}
}
// Return
return $field;
}
}
PS. I know I should add [] after $field[‘conditional_logic’] but then the code breaks.
Anyone can help me? 2 days in a row and my coffee machine is pretty much getting out of order…
Your conditional logic array is not set up correctly
$field['conditional_logic'] = array(
// the first nested array holds OR conditions
// there would be a separate nested array for each OR condition
array(
// the second nesting is AND conditions
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '==', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
),
array(
// this is an example, if there was an OR condition those would go here
)
);
Thanks for looking into my code.
But it still only outputs 1 condition (session / date combi) although I have 3 entries in the option page… and the other entries don’t have any effect
This is the condition output I get for for the field:
data-conditions="[[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200627"}],[]]"
The whole code is this:
// Apply conditions to fields
add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
function yl_check_booking_exeptions_session_1( $field ) {
// Retrieve option values. Date value should be like: 20200611 (unformatted)
if( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
while ( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
the_row();
$option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
$date = date_i18n('Ymd', strtotime($option_date));
$session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
if ( $session == '1' ) {
// Add the condition to the field
$field['conditional_logic'] = array(
// the first nested array holds OR conditions
// there would be a separate nested array for each OR condition
array(
// the second nesting is AND conditions
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
),
array(
// this is an example, if there was an OR condition those would go here
)
);
}
}
}
// Return
return $field;
}
I think I should FOREACH a part of the array. ? Is that a correct thinking?
Although this does not work, could you help me please? This is the last part in a long journey to develop my own booking system:
// Apply conditions to fields
add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
function yl_check_booking_exeptions_session_1( $field ) {
// Retrieve option values. Date value should be like: 20200611 (unformatted)
$rows = get_field('bookings_settings_disabled_exceptions', 'bookings');
if( $rows ) {
foreach ( $rows as $row ) {
$option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
$date = date_i18n('Ymd', strtotime($option_date));
$session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
$arrays =
array(
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
);
}
if ( $session == '1' ) {
$field['conditional_logic'] = array(
$arrays
);
}
}
// Return
return $field;
}
}
So that’s it?
I got everything right except the last piece of the puzzle and there’s nobody willing to help me trough?
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.