Home › Forums › Add-ons › Repeater Field › Sum Repeater fields
I’ve been banging my head against a wall on this for days and hoping someone can help!
I have a custom post type ‘shipment’, with a repeater field ‘products’ and two sub fields ‘product_name’ and ‘product_quantity’.
My aim is to get the sum total for each of the products shipped.
Any suggestions would be really appreciated!
$total_items = 0;
if (have_rows('shipment')) {
while (have_rows('shipment')) {
$total_items += get_sub_field('product_quantity');
}
}
echo $total_items;
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; ?>
Thanks John, Allison.
Unfortunately they didn’t do the trick. It might help if I reword my question with a real example.
I’m trying to make a table that shows the total number of products shipped across all shipments.
Shipment ‘123’ has the following products:
Baseball Cap * 1
T-Shirt * 2
‘Shipment’ is my CPT and my subfields within the ‘products’ repeater are ‘product_name’ and ‘product_quantity’.
John’s code seems like it would take the total quantity of ALL products shipped, not the quantity of each particular product.
$total_items = 0;
if (have_rows('shipment')) {
while (have_rows('shipment')) {
$total_items += get_sub_field('product_quantity');
}
}
echo $total_items;
Allison’s code seems like it should work but doesn’t refer to the ‘shipments’ CPT, so I’m not sure how it knows where to look for the ‘products’ repeater fields.
Thanks for the help anyway!
If you want to get the total of each product you could use an array, but I’m not sure how you’d use for your output.
$total_items = array();
if (have_rows('shipment')) {
while (have_rows('shipment')) {
$product_name = get_field('product_name;);
// initialize count for this product
if (!isset($total_items[$product_name])) {
$total_items[$product_name] = 0;
}
$total_items[$product_name] += get_sub_field('product_quantity');
}
}
echo '<pre>'; print_r($total_items); echo '</pre>';
The topic ‘Sum Repeater fields’ 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.