Home › Forums › Add-ons › Repeater Field › Sum Repeater fields › Reply To: Sum Repeater fields
Hi @charliechina,
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.