Hide field when date is selected (set in options page)
-
I have an option page in backend where I can select dates.
When one of these dates is selected in the datepicker in the frontend form I want to hide/disable other fields.
F.i. When 06-06-2021 is selected, the field of LUNCH TIME will disappear because on that day we only serve dinner…
Get my drift?
My option page outputs the needed value so I can create an IF STATEMENT… I simply don’t know how to hide a field via code in stead of within ACF of ACF Extended itself.
- This topic was modified 4 years, 5 months ago by Dennis Dallau.
-
Hello,
This is a question related to ACF, and not ACF Extended. This forum doesn’t aim to provide help on general development. If you need help on ACF development, try to post on the official Forum here: https://support.advancedcustomfields.com/
You can also join the ACF Slack Community for questions about ACF/ACFE (I’m also connected there). Here is an invitation link: https://join.slack.com/t/wpacf/shared_invite/enQtODc2MjczMzM3NzQ1LTQxNmY2ZGY3OTU2NDkxNTliYmNhMmY1NDMwZGM4NmYxNzgwZTI5MDUzMDFlMGQ5YzcwNDc0ZWM0MDVmODI4NmI
You should also try to provide a code that you already tried to implement in order to get some help, and not just ask the question without trying to achieve it by yourself.
I’ll answer your question and write the code for you tho, but you should consider joining a community for future questions about development.
If you want to add a conditional logic on date field, you’ll have to enable the
Value Equal
conditional logic on date fields. I’m kinda surprised that ACF doesn’t allow it out of the box, so I’ll add those conditions in the next ACF Extended patch. Meanwhile, you can do it by doing this in JavaScript:(function($){ if(typeof acf === 'undefined') return; acf.registerConditionForFieldType('equalTo', 'date_picker'); acf.registerConditionForFieldType('notEqualTo', 'date_picker'); acf.registerConditionForFieldType('patternMatch', 'date_picker'); acf.registerConditionForFieldType('contains', 'date_picker'); acf.registerConditionForFieldType('greaterThan', 'date_picker'); acf.registerConditionForFieldType('lessThan', 'date_picker'); })(jQuery);
Then enqueue the file using:
add_action('acf/enqueue_scripts', 'my_acf_enqueue_script'); function my_acf_enqueue_script(){ wp_enqueue_script('my-datepicker-condition', '/path/to/js/datepicker-condition.js', array('jquery')); }
Then you’ll have to hook on
acf/prepare_field
in order to change the conditional on the fields you want to hide. Useget_field('my_date_option', 'option')
in order to retrieve the data in the options page, then compare it against the actual form Date user input.// Apply conditions of my_field_1, my_field_2 & my_field_3 add_filter('acf/prepare_field/name=my_field_1', 'my_acf_date_condition'); add_filter('acf/prepare_field/name=my_field_2', 'my_acf_date_condition'); add_filter('acf/prepare_field/name=my_field_3', 'my_acf_date_condition'); function my_acf_date_condition($field){ // Retrieve option value. Value should be like: 20200611 (unformatted) $option_date = get_field('date', 'option', false); // Bail early if no option date found if(empty($option_date)) return $field; // Add the condition to the field $field['conditional_logic'] = array( array( array( 'field' => 'field_5ee1e4245a42d', // Field key of the date field of the form 'operator' => '!=', // If Value is different, then show the field 'value' => $option_date, // Compare against option page value ) ) ); // Return return $field; }
Regards.
I got it to work with the following code:
// Apply conditions of my_field_1, my_field_2 & my_field_3 add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_if_date_is_an_exeption'); add_filter('acf/prepare_field/name=bookings_field_time_session_2', 'yl_check_if_date_is_an_exeption'); function yl_check_if_date_is_an_exeption($field){ // Retrieve option value. Value should be like: 20200611 (unformatted) if( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) { while ( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) { the_row(); $date = get_sub_field('bookings_settings_disabled_date', 'bookings', false); $option_date = date_i18n('Ymd', strtotime($date)); } } // Bail early if no option date found if(empty($option_date)) return $field; // Add the condition to the field $field['conditional_logic'] = array( array( array( 'field' => 'field_5ed4178dd63d7', // Field key of the date field of the form 'operator' => '!=', // If Value is different, then show the field 'value' => $option_date, // Compare against option page value ) ) ); // Return return $field; }
The only problem (so to speak) is that this function overwrites the CONDITIONAL LOGIC set in ACF itself.
I guess there isn’t a way to ADD this to the logics rather than completely REPLACE it?
Hello,
The
$field['conditional_logic']
key is an array, so you can add a new OR rule (first level of the array[]
is OR, and second level is AND). Like this:$field['conditional_logic'][] = array( array( 'field' => 'field_5ed4178dd63d7', // Field key of the date field of the form 'operator' => '!=', // If Value is different, then show the field 'value' => $option_date, // Compare against option page value ) );
This way, the condition is added at what the conditions you added in the admin UI.
Regards.
I understand you completely but I’m not sure how to add a second (OR) array.
This does not work:
// Apply conditions of my_field_1, my_field_2 & my_field_3 add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_if_date_is_an_exeption'); add_filter('acf/prepare_field/name=bookings_field_time_session_2', 'yl_check_if_date_is_an_exeption'); function yl_check_if_date_is_an_exeption($field){ // Retrieve option value. Value should be like: 20200611 (unformatted) if( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) { while ( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) { the_row(); $date = get_sub_field('bookings_settings_disabled_date', 'bookings', false); $option_date = date_i18n('Ymd', strtotime($date)); } } // Bail early if no option date found if (empty($option_date)) return $field; // Add the condition to the field $field['conditional_logic'] = array( array( 'field' => 'field_5ed4178dd63d7', // >>>>>>>>>> Field key of the FIRST field of the form 'operator' => '!=', // If Value is different, then show the field 'value' => $option_date, // Compare against option page value array( 'field' => 'field_5ed4178dd63d7', // >>>>>>>>>> Field key of the OR field of the form 'operator' => '!=', // If Value is different, then show the field 'value' => $option_date, // Compare against option page value ) ) ); // Return return $field; }
Hello,
You need to read the PHP Array documentation: https://www.php.net/manual/en/function.array-push.php
Seeing my previous response, in your case, if you want to create
AND
operators:// OR $field['conditional_logic'][] = array( // AND array( 'field' => 'field_5ed4178dd63d7', 'operator' => '!=', 'value' => $option_date, ), // AND array( 'field' => 'field_5ed4178dd63d7', 'operator' => '!=', 'value' => $option_date, ) );
And if you want create
OR
operators:// OR $field['conditional_logic'][] = array( // AND array( 'field' => 'field_5ed4178dd63d7', 'operator' => '!=', 'value' => $option_date, ), ); // OR $field['conditional_logic'][] = array( // AND array( 'field' => 'field_5ed4178dd63d7', 'operator' => '!=', 'value' => $option_date, ) );
Note: You messed up your array structure in your code, watchout for enclosures.
Regards.
First of all let me say: “Thank you for helping me… really.”
Although I know I’m soooo close (because I see changes in showing fields when I select values) but I’m not there yet because it does not do it right.
Maybe it helps letting you exactly know what I’m trying to accomplish:
I have an option (repeater field) with date and session to disable bookings.
F.i.
15/06/2020 NO BOOKINGS the WHOLE day
18/06/2020 NO BOOKINGS session 1 (so session 2 is available)I check these DATE and SESSION values with 2 fields in my form:
OPTION DATE >> Checks the value in the DATEPICKER
OPTION SESSION >> Checks the value in SELECT SESSION FIELD (Session 1 for lunch or Session 2 for dinner)So If OPTION DATE != DATEPICKER VALUE && OPTION SESSION != SELECT SESSION FIELD show FIELD X (in my case the time field but can be anything)
Here’s what I have so far:
// Apply conditions to fields add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1'); add_filter('acf/prepare_field/name=bookings_field_time_session_2', '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); } } // Add the condition to the field $field['conditional_logic'] = array( array( 'field' => 'field_5ed4181bd63dc', // SELECT SESSION FIELD 'operator' => '!=', // If Value is different, then show the field 'value' => $session, // Compare against option page value ), array( 'field' => 'field_5ed4178dd63d7', // DATEPICKER FIELD 'operator' => '!=', // If Value is different, then show the field 'value' => $date, // Compare against option page value ) ); // Return return $field; }
- This reply was modified 4 years, 5 months ago by Dennis Dallau.
UPDATE:
The below code does exactly what I want… Meaning: When 23/06/2020 SESSION 1 should be disabled, the time field automatically dessapears when both values match.
Now the only struggle (and I guess it has to do with a FOREACH missing) is that only the last entry of the REPEATER FIELD (with disabled date and session) works. The once before are being ignored.
// 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(); if ( get_sub_field('bookings_settings_disabled_session', 'bookings') == '1' ) { $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); // 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; } } }
- This reply was modified 4 years, 5 months ago by Dennis Dallau.
- This reply was modified 4 years, 5 months ago by Dennis Dallau.
- This reply was modified 4 years, 5 months ago by Dennis Dallau.
I’m getting pretty frustrated here… Nomather what I change in the code…. It will always output only 1 condition.
f.i.:
data-conditions="[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200627"}]"
even though I have set 2 x date + session (in the repeater field) as conditions.// 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 ) { $rows = get_field('bookings_settings_disabled_exceptions', 'bookings'); // Retrieve option values. Date value should be like: 20200611 (unformatted) if ($rows) { foreach($rows as $row) { $option_date = $row['bookings_settings_disabled_date']; $date = date_i18n('Ymd', strtotime($option_date)); $session = $row['bookings_settings_disabled_session']; // 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; } }
HEEEEEEELLLLPPPPPP I’m too close to let go now ??
Hello,
Unfortunately I don’t have time to get in your code, test it & rewrite the whole thing. Again, your question is related to general PHP/ACF development, and this forum is dedicated to ACF Extended. Please join a development community in order to get help with general development questions.
It’s getting frustrating for me too, because you keep asking general development questions and it seems like you didn’t read my previous answers: You should check the documentation of the PHP Array in order to understand how to increment it. As I said, you should use
$field['conditional_logic'][]
in order to increment the array in your foreach. If you keep using$field['conditional_logic']
, without the[]
at the end, you keep rewriting the conditional logic in each row of yourforeach()
.Regards.
Yes I’m still trying and no I still haven’t found the answer.
I have posted multiple times on the ACF forms, I even created an official ACF ticket, I posted multiple times on Stack…
Really don’t understand why people are not willing to help me with the few lines of code but I guess that’s just the way it works ??
My final try and then I have to quit. Not because I want, but because I have to because the final key is missing to complete the puzzle.
What if I send you $75,- via PayPal (up front) would you then help me?
This working code:
// Apply conditions to fields add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions'); function yl_check_booking_setting_exceptions($field){ if ( have_rows('booking_setting_exceptions', 'booking_settings') ) { while ( have_rows('booking_setting_exceptions', 'booking_settings') ) { the_row(); if (get_sub_field('booking_setting_exceptions_session', 'booking_settings') == '1' ) { $date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings'))); // Bail early if no option date found if (empty($date)) { return $field; } // Add the condition to the field $field['conditional_logic'] = array( array( array( 'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form 'operator' => '==', // If Value is different, then show the field 'value' => '1', // Compare against session option page value ), array( 'field' => 'field_5ed4178dd63d7', // Time field session 1 in the form 'operator' => '!=', // If Value is different, then show the field 'value' => $date, // Compare against date option page value ) ) ); } } } // Return return $field; }
gives me this data-condition output:
data-conditions="[[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200625"}]]"
20200625 is the last date (row) of 3 rows in the repeater field.
I simply want this part
[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200625"}]
multiple times separated with comma so all conditions will be active.Can I hire you this? Someone with your knowledge of ACF should fix this in no time ??
If not… Anybody else around I can hire for this?
- The topic ‘Hide field when date is selected (set in options page)’ is closed to new replies.