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.

    if ( have_rows( 'products' ) ) :
    	$total = 0;
    	while ( have_rows( 'products' ) ) : the_row(); ?>
    		     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; ?>