Support

Account

Home Forums Front-end Issues Populate select field dynamically based on user choice

Unread

Populate select field dynamically based on user choice

  • The code below displays a TIME field dynamically based on the user selected date and session.

    If the user selected DATE and SESSION match the DATE and SESSION subfields in the option page >> Hide the field.

    Now my question is this:
    Is this logic also possible for populating a select field dynamically? So if a user selects DATE X >> SESSION FIELD has option 1 & 2, but if a user selects DATE Y >> SESSION FIELD only has option 1.

    Note: I now how to dynamically populate a select field but not related to the above condition example.

    Here’s the 100% working code for hiding/showing fields based on dynamic conditions. It’s kinda related so I thought: Let’s share…

    add_filter('acf/prepare_field/name=booking_time_session_1', 'yl_check_booking_setting_exceptions_session_1');
    function yl_check_booking_setting_exceptions_session_1($field){
        
        // Reset conditions
        // Warning: Conditions set in the UI won't be compatible!
        $conditions = array();
    
        // Creating Base Condition
        $conditions[] = array(
            array(
                'field'     => 'field_5ed4181bd63dc',
                'operator'  => '==',
                'value'     => '1',
            )
        );
    
        if(have_rows('booking_setting_exceptions', 'booking_settings')):
            while(have_rows('booking_setting_exceptions', 'booking_settings')): the_row();
            
                // Check: Booking Settings Exceptions Session
                if(get_sub_field('booking_setting_exceptions_session') == '2')
                    continue;
                
                $date = date_i18n('Ymd', strtotime(get_sub_field('booking_setting_exceptions_date', 'booking_settings')));
                
                // Check: Booking Settings Exceptions Date
                if(empty($date))
                    continue;
                
                // Adding a new AND condition inside Base condition, at the index = 0
                $conditions[0][] = array(
                    'field'     => 'field_5ed4178dd63d7',
                    'operator'  => '!=',
                    'value'     => $date,
                );
            
            endwhile;
        endif;
    
        // Setting the full condition
        $field['conditional_logic'] = $conditions;
         
         return $field;
        
    }
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.