Support

Account

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

Solved

Populate Field Value with Sum of Two Other Fields

  • I would like to populate the value of a field with the sum of two other numeric fields in the same post. For example:

    field1 + field2 = field3 –> on insert or update of post.

    So any time I add or update a post, it does the calculation and adds/update field3 with the result.

    All fields are numeric acf.

    Is there a function that exists for this, or one that I can modify to make this work?

    I prefer to do the calculation “on post insert/update” rather than on the front-end since I have other calculations to perform on the front-end.

  • There is a filter that runs when acf inserts or updates fields https://www.advancedcustomfields.com/resources/acf-save_post/

    You would create a filter function and in this function get the values from the 2 fields and then update the 3rd field https://www.advancedcustomfields.com/resources/update_field/

  • John,

    Thanks for pointing me to the two functions as a starting point. I was able to put together the following, and it worked perfect. Appreciate the help.

    Code:

    function my_acf_save_post( $post_id ) {
        
    // get new value
    $costInDollars = get_field('cost_in_dollars');
    $discountInDollars = get_field('discount_in_dollars');
        
    // do something
    $totalCostInDollars = $costInDollars - $discountInDollars;
    update_field('total_cost_in_dollars', $totalCostInDollars, $post_id);
    	
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • Hi, is this for frontend?
    I need to do the same in the Backend, like this thread
    https://support.advancedcustomfields.com/forums/topic/how-to-create-a-field-as-sum-of-two-other-fields/

    What type of field should I use for the 3rd field?
    Thanks!

  • Would I need to do anything differently to get this to work with a repeater field inside flexible content?

    I was able to get it to work fine outside of the repeater. Once I put it inside the repeater I used get_sub_field instead of get_field but that didn’t seem to do anything. Any ideas?

    Thanks in advance!

  • You would need to loop over the flex field and loop over the repeater, just like you if you were going to display the flex/repeater on the front end of the site. Then use get_sub_field() and update_sub_field() https://www.advancedcustomfields.com/resources/update_sub_field/

  • 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.

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

You must be logged in to reply to this topic.