Display sum of item quantity in PDF export

I’m having issues displaying total quantity of all items at the bottom of my PDF export script I’m using with my WordPress site.

I’ve been able to display total quantity of each item using

<td class="qty"><?php echo esc_html( $item->get_quantity() ) ?></td>

Then I want to calculate the sum of all item quantities and show that as a single figure at the bottom of the table

Would I use

echo sum_array ( $item->get_quantity() )


I hope you are showing each quantity via some loop, if yes then before loop define a variable like

$sum = 0;

Now change <td> code like this (inside loop):

<td class="qty">
     $sum += (int)$item->get_quantity(); 
     echo esc_html( $item->get_quantity() ) ?>

And now in the end use this $sum variable to show total quantity.

<?php echo $sum;?>

Leave a Reply

Your email address will not be published. Required fields are marked *