Support

Account

Home Forums Add-ons Repeater Field Save sum of repeater filed into another filed

Solved

Save sum of repeater filed into another filed

  • Hi!
    I want to save the sum of a repeater filed into another filed.
    Im trying to get this code to work without any success. What is wrong with it?

    Thanks!

    function my_acf_update_totalexpenses($post_id)
    {
    
       $total = get_sub_field('list'); $totaltasks = count($tasks);
    	$totaltaskpercentage = 0;
    		while ( have_rows('list') ) : the_row(); 
    			$totaltaskpercentage += intval( get_sub_field('expense_'));
    		endwhile; 
    	$grouptaskpercentage = $totaltaskpercentage / $totaltasks;
    	echo round($grouptaskpercentage);
    	$value = $total;
        $field_name = "totalexpenses";
        update_field($field_name, $value, $post_id);
    }
    add_action('save_post', 'my_acf_update_totalexpenses');
  • try using acf/save_post instead of save_post hook

    
    add_action('acf/save_post', 'my_acf_update_totalexpenses', 20);
    
  • Thanks for the answer, but the change didn’t work.

  • Try using the field key instead of the field name when updating.

  • I tried this with no difference

    function my_acf_update_totalexpenses($post_id)
    {
    
       $total = get_sub_field('list'); $totaltasks = count($tasks);
    	$totaltaskpercentage = 0;
    		while ( have_rows('list') ) : the_row(); 
    			$totaltaskpercentage += intval( get_sub_field('expense_'));
    		endwhile; 
    	$grouptaskpercentage = $totaltaskpercentage / $totaltasks;
    	echo round($grouptaskpercentage);
    	$value = $total;
        $field_name = "_totalexpenses";
        update_field($field_name, $value, $post_id);
    }
    add_action('acf/save_post', 'my_acf_update_totalexpenses', 20);
  • You need the field key. it will be something like “field_123abc5”.

  • Thanks! This is my filed key “field_5ca90e913bb43” Can you show me how do I add it?

  • 
    function my_acf_update_totalexpenses($post_id)
    {
    
       $total = get_sub_field('list'); $totaltasks = count($tasks);
    	$totaltaskpercentage = 0;
    		while ( have_rows('list') ) : the_row(); 
    			$totaltaskpercentage += intval( get_sub_field('expense_'));
    		endwhile; 
    	$grouptaskpercentage = $totaltaskpercentage / $totaltasks;
    	echo round($grouptaskpercentage);
    	$value = $total;
        update_field('field_5ca90e913bb43', $value, $post_id);
    }
    add_action('acf/save_post', 'my_acf_update_totalexpenses', 20);
    
  • unfortunately its still not working. I have tested with different fields just to be sure but it doesn’t work

  • What file have you put this function in?

  • I use the Code Snippets plugin to add it to the functions.php file

  • After reading your code more closely, there are several issues that I overlooked before:

    
    function my_acf_update_totalexpenses($post_id) {
    
      // ****************************************************************
      // $total is getting a sub field.
      // You can't get a sub field if you are not in have_rows() loop
      // there is no loop, so this will return nothing
      // it must be incorrect
      $total = get_sub_field('list');
      
      
      // *****************************************
      // what is this counting? $tasks does not exist yet
      // this will be 0 on PHP 5.6 and cause an error in on PHP 7
      $totaltasks = count($tasks);
    
      
      $totaltaskpercentage = 0;
      
      
      // add if and add $post_id to have_rows() call
      if (have_rows('list', $post_id)) {
        while (have_rows('list', $post_id)) {
          the_row();
          // ***********************************************************
          // is "expense_" the right sub field name? ending with an _?
          $totaltaskpercentage += intval(get_sub_field('expense_'));
        }  // end while_have_rows
      } // end if have_rows
      $grouptaskpercentage = $totaltaskpercentage / $totaltasks;
    
      
      // **********************************************************
      // I don't think this is right
      // the above code never modifies the value of $total
      // since $total starts with no value it will have no value here
      $value = $total;
      
      
      update_field('field_5ca90e913bb43', $value, $post_id);
    }
    add_action('acf/save_post', 'my_acf_update_totalexpenses', 20);
    
  • This code solved my need. The repeater filed is “expense” and the filed to which the total sum was saved is “total_Expenseslist”

    function my_acf_input_admin_footer() {
    	echo "<!--";
            $posts = get_posts(array(
                'post_type' => 'time_sheet',
                'post_status' => 'publish',
                'numberposts' => -1
            ));
            $records = Array();
            foreach ($posts as $post) {
                $id = $post->ID;
                $userdata = get_user_meta($post->post_author);
                var_dump($userdata);
            }
    	echo "-->";
    ?>
    <script type="text/javascript">
    (function($) {
    
        function sumExpenses() {
            var $fields = acf.findFields({
                name: 'expense'
            });
            totalExpenses = 0;
            $fields.each(function(dummy, field) {
                var v = parseFloat($(field).find('input').val().replace(/[\$\,\s]/g, ""));
                if (!isNaN(v)) {
                    totalExpenses += v;
                }
            });
            var $total = acf.findFields({
                name: 'total_Expenseslist'
            });
            $total.each(function(dummy, t) {
                $(t).find('input').val(totalExpenses);
            });
        }
    
        acf.addAction('ready_field/name=expense_', function(field) {
            field.$input().on('change', sumExpenses);
        });
    
        acf.addAction('append_field/name=expense_', function(field) {
            field.$input().on('change', sumExpenses);
        });
    
        // Make total expenses read-only
        acf.addAction('ready_field/name=total_Expenseslist', function(field) {
            field.$input().prop('readonly', true);
        });
    
    })(jQuery);	
    </script>
    <?php
    }
    
    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
    
Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘Save sum of repeater filed into another filed’ is closed to new replies.