Support

Account

Home Forums Add-ons Repeater Field Dynamically Populating Repeater

Solved

Dynamically Populating Repeater

  • Hello,

    I have a taxonomy and repeater field.

    In taxonomy field I am going to choose some treatments.

    In repeater field I am going to create prices for these treatments.

    Repeater (field_5bd1929afde25)
    Field 1: treatment_name (field_5bd192adfde26)
    Field 2: min. price
    Field 3: max. price

    function treatment_prices( $value, $post_id, $field ) {
        
    	// get terms
    	$terms = wp_get_post_terms( $post_id, 'treatment', array( 'orderby' => 'term_id' ) );
        
    	foreach ( $terms as $term ) {
    		$value[] = array(
    			'field_5bd192adfde26' => $term->name
    		);	
        }
    
        return $value;
    }
    add_filter('acf/load_value/key=field_5bd1929afde25', 'treatment_prices', 10, 3);

    For example I select 4 taxonomy terms and update post.
    I can see 4 treatment names which are populated dynamically in repeater field.

    There is no problem until here.

    When I enter the prices for this 4 treatments and update post, It adds same 4 treatment names fields again.

    How can I solve it?

    Also for example when I deselect 2 taxonomy terms in taxonomy field, I want them to be gone in repeater field. Is this possible? If yes, how can I solve it?

    I will be glad if you can help me.

  • The simplest fix is at the beginning of your function you need to see if the field already has a value.

    
    function treatment_prices( $value, $post_id, $field ) {
      if (!empty($value)) {
        return $value;
      }
    

    However, I don’t think that’s going to be your best solution for the future.
    Since you are dynamically adding the fields based on another field what you really need to do is to compare what’s already in the repeater with the selected terms. Add any taxonomies that were not there the last time and remove any that are no longer selected.

    
    function treatment_prices( $value, $post_id, $field ) {
      $terms = wp_get_post_terms($post_id, 'treatment', array( 'orderby' => 'term_id'));
      $selected_terms = array();
      $existing_terms = array();
      $new_value = array();
      // collect list of selected terms
      if ($terms) {
        foreach ($terms as $term) {
          // we are setting the key and value so that we can unset it if it already exists
          $selected_terms[$term->name] = $term->name
        }
      }
      // loop though the rows
      // collect list of existing terms
      // if the existing term is in selected terms then add it to the output
      if (is_array($value)) {
        foreach ($value as $row) {
          if (in_array($row['field_5bd192adfde26'], $selected_terms)) {
            // unset this from selected terms if it exists
            if (isset($selected_terms['$row['field_5bd192adfde26']])) {
              unset($selected_terms['$row['field_5bd192adfde26']]);
            }
            // add row to output
            $new_value[] = $row;
          }
        }
      }
      // loop over selected terms and add any new ones, if any
      if (count($selected_terms)) {
        foreach ($selected_terms as $name) {
          $new_value[] = array('field_5bd192adfde26' => $name);
        }
      }
      return $new_value;
    }
    
  • Thanks for your reply John.

    I tried your solution but min.price and max. price fields are missing. They appear blank after updating the post.

    Also, if I want to add an extra field (for example: term id) and dynamically populate it, how should I edit the function?

    Many thanks.

  • My code is really just an example.

    My code does not set a default value for other fields in the row. If you want of need that then you would add them near the end where the new rows are added. The same for any other fields/default values you want to add to each row.

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

The topic ‘Dynamically Populating Repeater’ is closed to new replies.