Support

Account

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

  • 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);