Support

Account

Home Forums Add-ons Repeater Field Dynamically populate repeater field with select field

Solved

Dynamically populate repeater field with select field

  • I have two repeater fields, an ‘items ordered’ repeater that contains an item select field, and another ‘items shipped’ repeater (that I’m trying to dynamically populate) which will contain the same item select field.

    ordered items repeater:
    ordered items repeater

    items shipment repeater:
    items shipment repeater

    When the shipment form is rendered, I want the item shipment repeater to pre-populate the “item ordered” select field with the selected “item ordered” from the purchase order repeater.

    So far I can get the shipment form to create the same number of rows that is in the order form, but I can’t get the select fields to match up, or the item descriptions. The shipment quantity is meant to be filled in by the user. I have also pre-populated the select dropdowns by using an inventory options page.

    select pre-population:

    
    function opo_item_select_options( $field ) {	
         $field['choices'] = array();
         if( have_rows('items_from_factory', 'inventory-options') ) {
              while( have_rows('items_from_factory', 'inventory-options') ) { 
                   the_row();
                   if(get_sub_field('is_module') == false){
                        $value = get_sub_field('item_id');
                        $label = get_sub_field('item_name'); 
                        $field['choices'][ $value ] = $label;
        	        }
               }	        
          }
          return $field;
    }
    add_filter('acf/load_field/name=opo_item_ordered', 'opo_item_select_options');	
    add_Filter('acf/load_field/name=item_shipped', 'opo_item_select_options');
    

    trying pre-populate the repeater with the select:

    function load_opo_items_to_ship( $value, $field, $post_id ){
         $opo_id = url_to_postid( get_permalink());
    
         $value = array();
         if( have_rows('po_information_purchase_order_items', $opo_id) ) {
              while( have_rows('po_information_purchase_order_items', $opo_id) ) { 
                   the_row();
                   $value[] = array(
                      'item_shipped' => get_sub_field('field_5c1c21500368d'),
                      'item_description' => get_sub_field('field_5c1c21500368d')
                   );
              }
         }
         return $value;
    }
    add_filter('acf/load_value/key=field_5c76cc36ace4a', 'load_opo_items_to_ship', 10, 3);
  • Well I solved the issue. I modified the priority of my filters, and instead of using the field name, I used the field keys. Code ended up like this:

    I changed the priority on the select field population function to run before the repeater population

    add_filter('acf/load_field/name=item_shipped', 'opo_item_select_options', 9, 1);

    function load_opo_items_to_ship( $value, $field, $post_id ){
         $opo_id = url_to_postid( get_permalink());
    
         $value = array();
         if( have_rows('po_information_purchase_order_items', $opo_id) ) {
              while( have_rows('po_information_purchase_order_items', $opo_id) ) { 
                   the_row();
                   $value[] = array(
                      'field_5c76cc4bace4b' => get_sub_field('opo_item_ordered'),
    	          'field_5c76ccb3ace4c' => get_sub_field('opo_item_description'),
                   );
              }
         }
         return $value;
    }
    add_filter('acf/load_value/key=field_5c76cc36ace4a', 'load_opo_items_to_ship', 10, 3);
  • Well, I solved the issue. I modified the priority of my filters, and instead of using the field name, I used the field keys. Code ended up like this:

    I changed the priority on the select field population function to run before the repeater population

    add_filter('acf/load_field/name=item_shipped', 'opo_item_select_options', 9, 1);

    function load_opo_items_to_ship( $value, $field, $post_id ){
         $opo_id = url_to_postid( get_permalink());
    
         $value = array();
         if( have_rows('po_information_purchase_order_items', $opo_id) ) {
              while( have_rows('po_information_purchase_order_items', $opo_id) ) { 
                   the_row();
                   $value[] = array(
                      'field_5c76cc4bace4b' => get_sub_field('opo_item_ordered'),
    	          'field_5c76ccb3ace4c' => get_sub_field('opo_item_description'),
                   );
              }
         }
         return $value;
    }
    add_filter('acf/load_value/key=field_5c76cc36ace4a', 'load_opo_items_to_ship', 10, 3);
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.