Support

Account

Home Forums Add-ons Repeater Field Trying to get values from nested repeater in admin area

Solved

Trying to get values from nested repeater in admin area

  • Hello,
    I have a nested repeater field and I’m trying to get some values from the nested repeater field on button click via AJAX.

    NOTE: I’ve tried yesterday to create this ticket but some error occurred. If there is a copy of this topic please delete it. This is the original

    Inside my repeater field “day_repeater” I have a date field “date” and a repeater field “accommodation” and inside the “accommodation” repeater field I have a text field “notes” and a post object field for my custom post type “accommodations”.

    I’ve created an AJAX script that runs the PHP function “email_sender” to get the values printed in the console.log (later I’m going to use the values to send them via email to my customers – there is a commented code for that).

    The problem is that I can’t get the values from my repeater fields (field “date” in repeater field “day_repeater” and field “notes” in nested repeater field “accommodation”) instead it returns null in the console log. I’ve tried the same code on the front end and I get all the values when I echo them.

    Here is my PHP code email-sender.php

    
    // Get values from repeater fields and send email to partner with Accommodation info
    add_action('wp_ajax_email_sender', 'email_sender');
    function email_sender($field) {
        if( have_rows('day_repeater') ):
            // loop through the rows of data
            while ( have_rows('day_repeater') ) : the_row();
    
            $date = get_sub_field('date'); // I need values from this field
            
                // LOOP FOR NESTED REPEATER FIELD
                if ( get_field('accommondation') ):
                    while( has_sub_field('accommondation') ) : the_row();
                    
                    $notes = the_sub_field('notes'); // I need values from this field
    
                    endwhile;
                endif;
            endwhile;
        endif;
    
        $post_id = $_POST["post_id"];
        $args = array(
            'p' => $post_id,
            'numberposts'=> -1,           // Fetch all posts...
            'post_type'=> 'accommodations',      // from the 'accommodation' CPT...
        );
    
        $accommodations = new WP_Query( $args );
    
        if ( $accommodations->have_posts() ) : while ( $accommodations->have_posts() ) : $accommodations->the_post();
            // Get values from CPT Accommodations
            $title = get_field('title');
            $partnerName = get_field('partner_name');
            $partnerEmail = get_field('partner_email');
            wp_reset_postdata();
            endwhile;
        endif;
    
        // Values that get printed in the console log
        $values = array(
            'title' => $title,
            'partnerName' => $partnerName,
            'notes' => $notes,
            'date' => $date
        );
        wp_send_json($values);
    
        /* // Send email to partner
            $subject = 'Test mail';
    
            ob_start(); ?>
    
            <?php echo ($title); ?>
    
            <?php echo ($partnerName); ?>
    
            <?php echo ($notes); ?>
    
            <?php
            $message = ob_get_contents();
            ob_end_clean();
            wp_mail( $partnerEmail, $subject, $message );
        */
        ?>
    <?php }
    //end
    
    //Add button to trigger AJAX script
    function my_acf_email_button($field) {
      echo '<div class="acf-field"><button type="submit" id="email_send" class="button">Send email</button></div>';
    	return $field;
    }
    add_filter('acf/prepare_field/name=send_inquery', 'my_acf_email_button');
    //end
    
    // Localize email-sender.js script to wordpress admin-ajax.php
    function js_email_send() {
      wp_enqueue_script ("ajax-email-sender", get_stylesheet_directory_uri() . "/inc/assets/js/email-sender.js", array('jquery')); 
      //ajax_email_sender will use to print admin-ajaxurl in email-sender.js
      wp_localize_script('ajax-email-sender', 'ajax_email_sender', array('ajaxurl' =>admin_url('admin-ajax.php')));
    } 
    add_action("admin_enqueue_scripts", "js_email_send");
    //end
    

    and here is my AJAX script email-sender.js

    
    jQuery(document).on( 'click', '#email_send', function( slanje ){
        slanje.preventDefault();
        // Picks post ID from relationship field
        var post_id = jQuery('.acf-row .acf-fields .acf-field .acf-input select option:selected').val();
    
        jQuery.ajax({
            url: ajax_email_sender.ajaxurl,
            type: "POST",
            dataType: 'json',
            data: {
                action: 'email_sender', // PHP function that is run
                post_id: post_id,
            },
            success: function(data){
                console.log(data)
            },
            error: function(error){
                console.log(error)
            },
        });
    });
    

    Also here is a screenshot of the console log and the repeater field
    Console log
    Console log

    Repeater field
    Repeater field

    Any idea how to get the values from the “date” and “note” fields in the console?
    I’m out of ideas and I don’t know what to try and where to look anymore. :/

    Thank you in advance!
    Kristijan

  • The problem is that ACF does not know what post you want to get the field from

    
    add_action('wp_ajax_email_sender', 'email_sender');
    function email_sender($field) {
       // move this to the top
        $post_id = $_POST["post_id"];
    
        if( have_rows('day_repeater', $post_id) ):
            // loop through the rows of data
            while ( have_rows('day_repeater', $post_id) ) : the_row();
    
            $date = get_sub_field('date'); // I need values from this field
            
                // LOOP FOR NESTED REPEATER FIELD
                if ( get_field('accommondation') ):
                    while( has_sub_field('accommondation') ) : the_row();
                    
                    $notes = the_sub_field('notes'); // I need values from this field
    
                    endwhile;
                endif;
            endwhile;
        endif;
    
    
  • I was able to get the values but the problem is that I have multiple rows in each repeater field and I get only the values from the last repeater field.

    Is there a way how I can get the values from a specific repeater row?

    For example when I push the button that I prepared to the ACF field. I should get only the values from that specific row where the button is located.

    I’ve tried to search for every possible solution but I didn’t find anything useful.

    Thank you in advance!

  • The code I originally posted was not 100%, I missed an error when copying your original code

    
    add_action('wp_ajax_email_sender', 'email_sender');
    function email_sender($field) {
       // move this to the top
        $post_id = $_POST["post_id"];
    
        if( have_rows('day_repeater', $post_id) ):
            // loop through the rows of data
            while ( have_rows('day_repeater', $post_id) ) : the_row();
    
            $date = get_sub_field('date'); // I need values from this field
            
                // LOOP FOR NESTED REPEATER FIELD
                if ( get_field('accommondation') ):
                   // next line changed
                    while( have_rows('accommondation') ) : the_row();
                    
                    $notes = the_sub_field('notes'); // I need values from this field
    
                    endwhile;
                endif;
            endwhile;
        endif;
    

    There isn’t really a way to get the specific row other than to get the values from that row using JavaScript and submit them as part of your ajax request.

  • Thank you for the response. I was able to solve the problem with AJAX and it works perfectly. 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Trying to get values from nested repeater in admin area’ is closed to new replies.