Support

Account

Home Forums Add-ons Repeater Field Updating sub-subfield not working

Solving

Updating sub-subfield not working

  • Hello!

    With an Ajax function (onclick) I’m trying to update a field in a nested repeater, meeting some conditions, but it is not working. Have tried many ways, but I can’t get it to work.

    Here’s my code:

    function updatemogelijkheid(){
      $mogelijkheid = $_POST['mogelijkheid'];
      $vestiging = $_POST['vestiging'];
    
      if( have_rows('vestigingen', 'option') ) : while ( have_rows('vestigingen', 'option') ) : the_row();
        $vestiging_naam = get_sub_field('vestiging_naam');
    
        if($vestiging_naam == $vestiging){
          if( have_rows('mogelijkheden', 'option') ) : while ( have_rows('mogelijkheden', 'option') ) : the_row();
            $datumentijd = get_sub_field('datum_en_tijd');
            $gereserveerd = get_sub_field('gereserveerd');
    
            if($datumentijd == $mogelijkheid) :
                update_sub_field('gereserveerd', 'gereserveerd' ? '0' : '1', 'option');
            endif;
          endwhile; endif;
        }
      endwhile; endif;
    
      die();
    }
    add_action( 'wp_ajax_updatemogelijkheid', 'updatemogelijkheid' );
    add_action( 'wp_ajax_nopriv_updatemogelijkheid', 'updatemogelijkheid' );

    Can some one please help me with this?

  • Have you tried using the field key when doing the update? If the field does not already have a value then update_field() will fail to save the value. See the information on using the field key on this page https://www.advancedcustomfields.com/resources/update_field/

  • Thanks for your answer. Yes, I already tried to update the field based on the key. Also this doesn’t work.

  • I had a look at the database, how the values are stored. I’ve found a work around by doing this:

    update_option( 'options_vestigingen_'.$parent_i.'_mogelijkheden_'.$child_i.'_gereserveerd', 'a:1:{i:0;s:2:"ja";}' );

  • Sadly my work around doesn’t work for the function ‘delete_row()’.

    Based on a date in one of the subfields, I check if a date is past the current date and I want to remove that specific row. Only the function delete_row does not work on nested repeaters?

    Here’s my code:

    function deletePastRows() {
    	date_default_timezone_set('Europe/Amsterdam');
    	$currenttime = new DateTime();
    
    	$parent_i = 0;
      if( have_rows('vestigingen', 'option') ) : while ( have_rows('vestigingen', 'option') ) : the_row();
    
          $child_i = 0;
          if( have_rows('mogelijkheden', 'option') ) : while ( have_rows('mogelijkheden', 'option') ) : the_row(); $child_i++;
    
    				$rowtime = get_sub_field('datum_en_tijd');
    				$rowtime = new DateTime($rowtime);
    
    				if($rowtime < $currenttime) {
    						delete_row( 'mogelijkheden', $child_i, 'option' );
    				}
    
          endwhile; endif;
    
        $parent_i++;
      endwhile; endif;
    
    }
  • unfortunately, I don’t have any experience with update_row or delete_row and I’d say that you’re problems are definitely due to having nested repeaters. More than likely the nesting will need to be reflected in the $selector argument, but I don’t really know for sure.

    You might get more help if you open a support ticket on this issue and see if they can help you. https://support.advancedcustomfields.com/new-ticket/

    Please post anything you find here since it will help others with the same problem.

  • Looks like the form on the new ticket page is not sending properly, can’t submit a ticket.

    Is there someone else who could help?

  • You can send an email to [email protected] if the form is not working for you.

  • @rinse06

    Did you get a solution?
    And if so, what is it.
    I really like to know, because I’m having difficulties adding a new row in a nested repeater. Seems like nested repeaters are not very well covered by ACF.

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

You must be logged in to reply to this topic.