Support

Account

Home Forums Backend Issues (wp-admin) Pre-Populating a single field within a Repeater

Helping

Pre-Populating a single field within a Repeater

  • Hopefully the following makes sense.

    I am dynamically populating a repeater field_5c5324621ceb5 sub-field value of TITLE field_5c5324791ceb8 on a single product page (CPT) with specification titles that have been populated on said products Range (taxonomy). This means that every product within this range gets the same specification titles in the backend. This is working great.

    However, I also have a empty sub-field in the same repeater row called VALUE field_5c5324621ceb7 which the user needs to be able to fill in with the right values, as the VALUES will be different for every product.

    I am using the following code:

    $_taxonomy = 'product_cat';
     $_taxTerms = get_the_terms($post_id , $_taxonomy);
            
            // GET THE RANGE FROM THE PRODUCT ID
            if ( !empty( $_taxTerms ) ){
                $_taxTerm = array_shift( $_taxTerms );
                $_range = get_field('range_selection', $_taxTerm);
    
                if ( $_range ) : 
                    foreach ( $_range as $_range ) :
                        $_thisRange = $_range;
                    endforeach;
                endif;
            } 
    
            $_specRange = 'specification_field';
                
                if( get_post_type( $post_id ) == 'product' ){
                    $value  = array();
    
                    if( have_rows('specification_field', $_range) ) {
                        while ( have_rows('specification_field', $_range) ) { the_row(); 
                            $_spec = get_sub_field('spec');
                            $_title = $_spec->name;
    
                            $value[] = array(
                                'field_5c5324791ceb8' => $_title
                            );
                        }   
    
                    }
                }
       return $value;
    }
    add_filter('acf/load_value/key=field_5c5324621ceb5',  [$this, 'afc_load_my_repeater_value'], 10, 3);

    When you fill in the VALUE sub-field – nothing gets saved using the above code. I have also tried adding…

    if ($value != NULL) {
    return $value;
    }

    …to the beginning of the above function, which lets you save whatever was inputted into VALUE. But this also means that if I add new specification title to the Range, then a new row does not get added to the single product page if VALUES have been saved.

    Basically I need to be able to pre-populate the TITLE sub-field field_5c5324791ceb8 with specification titles from the Range taxonomy. And I need the VALUE sub-field to be empty and for the user to be able to input whatever they like into it on the single product page.

    I hope this all makes sense. Any help would be much appreciated.

  • The main issues is that you are repopulating the values of the “Title” field after saving. This is removing the values. You have 2 choices here.

    The first is to not populate the “title” in each row if the field already has values. You need to test the value pasted to your filter by ACF and if it already contains values then you do not run your code.

    The second is to somehow loop through the existing values and repopulate the title sub field. I’m sure it’s possible, but complex, to do something like this.

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

The topic ‘Pre-Populating a single field within a Repeater’ is closed to new replies.