Or in a function in functions.php.. You can even put them in a function inside a custom plugin if you want.
All that’s needed is that they or the call to a function in which they are calculated are placed before the echo $total.
Easiest is indeed to put it in a page template where you want it to be shown.