Home › Forums › Add-ons › Repeater Field › Sum of select values in a repeater
I want to add up select values from multiple repeater rows and display the sum. I have been playing with array_sum() and nested loops but haven’t been able to make it work. The snippet below will fetch and display the value from the last row value. If anyone has done this and could offer insight to this approach, I would be very grateful.
<?php
$total = 0;
if( have_rows('packaging_sessions') ): ?>
<table>
<caption>Packaging Sessions
</caption>
<thead>
<tr>
<th>Quantity × Capacity
</th>
<th>Volume
</th>
</tr>
</thead>
<tbody>
<?php while( have_rows('packaging_sessions') ): the_row();
// vars
$packaging_capacity = get_sub_field('packaging_capacity'); // The select subfield in the repeater (size)
$package_capacity_value = esc_attr($packaging_capacity['value']); // The select field's values
$package_capacity_label = esc_html($packaging_capacity['label']); // The select field's labels
$packaging_quantity = get_sub_field('packaging_quantity'); // The quantity of packaged units
$finished_package = $packaging_quantity * ($package_capacity_value * .001); // The quantity x size
// Sum the select field's values
$repeater = get_field('packaging_sessions');
foreach ($repeater as $row) {
$finished_package;
$total++;
}
?>
<tr>
<td><!-- Quantity & Capacity -->
<?php echo (int)$packaging_quantity . " × " . $package_capacity_label ?>
</td>
<td><!-- Volume -->
<?php echo $finished_package; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<!-- Sum volume of all package combinations -->
<?php echo 'Total packaged volume is ' . $total; ?>
I managed to solve it and am posting for future readers. The issue was that I was focusing on the wrong variable. I was trying to loop through the subfield value but it was already being looped and calculated to a different variable.
<?php
if( have_rows('packaging_sessions') ): ?>
<table>
<caption>Packaging Sessions
</caption>
<thead>
<tr>
<th>Quantity × Capacity
</th>
<th>Volume
</th>
</tr>
</thead>
<tbody>
<?php
$total_finished_packages = 0;
while( have_rows('packaging_sessions') ): the_row();
// vars
$packaging_capacity = get_sub_field('packaging_capacity'); // The select subfield in the repeater (size)
$package_capacity_value = esc_attr($packaging_capacity['value']); // The select field's values
$package_capacity_label = esc_html($packaging_capacity['label']); // The select field's labels
$packaging_quantity = get_sub_field('packaging_quantity'); // The quantity of packaged units
$finished_package = $packaging_quantity * ($package_capacity_value * .001); // The quantity x size
?>
<tr>
<td><!-- Quantity & Capacity -->
<?php echo (int)$packaging_quantity . " × " . $package_capacity_label ?>
</td>
<td><!-- Volume -->
<?php echo $finished_package; ?>
</td>
</tr>
<?php
// Iterate through the capacities and quantities
$total_finished_packages += $packaging_capacity * $packaging_quantity ;
?>
<?php endwhile; ?>
</tbody>
</table>
<!-- Sum volume of all package combinations -->
<?php echo 'Total packaged volume is ' . $total_finished_packages ; ?>
The topic ‘Sum of select values in a repeater’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.