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!
© 2022 Advanced Custom Fields.