Support

Account

Home Forums General Issues Populate Field Value with Sum of Two Other Fields Reply To: Populate Field Value with Sum of Two Other Fields

  • Thanks. I actually have Repeater > Flexible Content > Repeater. After working with the information from the support article you posted I got it working. Here is what worked.

    
    function my_acf_save_post( $post_id ) {
    if( have_rows('content_panels') ): 
    	$parent_i = 0;
    	while( have_rows('content_panels') ): the_row();
    	 $parent_i++;
     		if( have_rows('panel_contents') ):
    			while( have_rows('panel_contents') ): the_row();
    				if( have_rows('content_columns') ):
    				$child_i = 0;
    					while ( have_rows('content_columns') ) : the_row();	
    					$child_i++;
    					// get new value
    					$topMargin = get_sub_field('column_margin_top');
    					$bottomMargin = get_sub_field('column_margin_bottom');
    						if($topMargin || $bottomMargin):
    						$totalMargin = $topMargin + $bottomMargin;	
    						update_sub_field('total_margin', $totalMargin, $post_id);
    						endif;
    					endwhile;
    				endif;
    			endwhile;
    		endif;
    	endwhile;
    endif;
    }
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    Is there a way to optimize what I have or is this correct? I just don’t want it to cause issues once I get into a production environment. Thanks again.