Home › Forums › Add-ons › Repeater Field › Sum Repeater fields › Reply To: Sum Repeater fields
The strategy I used here was to set a counter to zero and then with each round of the while loop to add the new value to that variable. At the end, you can take that variable and output it where/within the markup needed.
+= is what is called an arithmetic operator.
Intval will make sure that the variable is brought in as an integer.
<?php if ( have_rows( 'products' ) ) : $total = 0; while ( have_rows( 'products' ) ) : the_row(); ?> <?php the_sub_field( 'product_name' ); ?>: <?php the_sub_field( 'product_quantity' ); $total += intval( get_sub_field( 'product_quantity' ) ); endwhile; ?> <p><?php echo esc_attr( $total ); ?></p> <?php endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.