Support

Account

Home Forums Add-ons Repeater Field Getting values from nested repeater in admin area

Solved

Getting 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.

    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(); ?>
    
            <p><?php echo ($title); ?></p>
            <p><?php echo ($partnerName); ?></p>
            <p><?php echo ($notes); ?></p>
    
            <?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

  • Yes, I double posted it by mistake. You can delete this post.

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

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