Support

Account

Home Forums Add-ons Repeater Field Repeater field value not working with Gravity Forms Reply To: Repeater field value not working with Gravity Forms

  • Nevermind just figured it out, I nested the forloops

    Heres my code if it helps someone else, just trying to stop it from returning values that dont relate to the current post:

    function acf_event_cost_options( $form ){
    
        foreach ($form['fields'] as &$field) {
    
            if(strpos($field->cssClass, 'acf_event_cost_options') === false)
                continue;
            //This only auto-populates a field which has the class dynamicfield. If it doesn't then, continue ! Don't forget to add a class to the field you want to dynamically populate in your form.
    
            global $post;
            $id = $post->ID; 	//  Important - this is what tells the form to only use values from the individual post, not all posts
    
            global $wpdb;
            $rows_name = $wpdb->get_results($wpdb->prepare(
                "
                SELECT *
                FROM wp_postmeta
                WHERE post_id = %d AND meta_key LIKE %s 
                ",
                $id,
                'event_cost_structure_%_option_name'  //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
            ));
    
            $rows_price = $wpdb->get_results($wpdb->prepare(
                "
                SELECT *
                FROM wp_postmeta
                WHERE post_id = %d AND meta_key LIKE %s 
                ",
                $id,
                'event_cost_structure_%_option_price'  //enter the actual name of your repeater field and sub-field names, the % is a variable that represents the row number
    
            ));
    
            $choices = array(array('text' => 'Please Choose an Option', 'value' => ' ', 'price' => ' '));
            //Default choice for dynamic select field. Value is blank, so if it's required it won't validate, change the text to what you want it to be, e.g. 'Please Choose:'
    
            if( $rows_name || $rows_price ) {
                foreach ($rows_name as $row_name) {
                    foreach ($rows_price as $row_price) {
                        //If there are results, for each result, find the 'repeater row number'
                        preg_match('_([0-9]+)_', $row_name->meta_key, $matches);
    
                        $meta_key = 'event_cost_structure_' . $matches[0] . '_option_name';
                        $value = get_post_meta($post->ID, $meta_key, true);
                        //Get the subfield value from the row.
    
                        //If there are results, for each result, find the 'repeater row number'
                        preg_match('_([0-9]+)_', $row_price->meta_key, $matches);
    
                        $meta_key_price = 'event_cost_structure_' . $matches[0] . '_option_price';
                        $value_price = get_post_meta($post->ID, $meta_key_price, true);
                        //Get the subfield value from the row.
    
                        $choices[] = array('text' => $value . ' $' . $value_price . '.00', 'value' => $value, 'price' => $value_price);
                        //Create an option array for each subfield value.
                    }
                }
            }
    
            $field->choices = $choices;
            //Once we've looped through all of the subfields values and created all of our options, we assign the $choices array (which has all of our new options) to the $field choices property.
    
        }
    
        return $form;
        //Display the new dynamically populated form.
    }