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 ; ?>
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.