Tell us what you are trying to do?
Hi, I'm displaying a list of custom post types called Aid Items; each Aid Item post has a field for a quantity and a field for cost. I'm trying to calculate and display the total amount for all Aid Items in the loop. (So the total needs to be a sum of each items cost * quantity.)
Is there any documentation that you are following?
Yes, I followed the solution provided in this post:
https://toolset.com/forums/topic/code-for-calculating-sum-of-another-group-of-fields/
...and succeeded in calculating the sum of each Aid Item's cost (but without multiplying the cost times the quantity). I tried to figure it out, but haven't had any success.
Is there a similar example that we can see?
You can see an example here:
hidden link
Note that the total of $2363.77 would be correct *if* each item had a quantity of one.
Here's the relevant part of my View loop:
<wpv-loop>
<td>[types field='item-qty' format='FIELD_VALUE'][/types]</td>
<td>$[types field='item-cost' format='FIELD_VALUE'][/types][add-to-total name='item-cost'][types field='item-cost' format='FIELD_VALUE'][/types][/add-to-total]</td>
</wpv-loop>
...and the part of the view that displays the total:
[show-total name='item-total'][/show-total]
Here's my custom code that creates the shortcodes shown above:
global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
$total[$atts['name']] += wpv_do_shortcode($content);
}
add_shortcode('add-to-total', 'add_total_shortcode');
function show_total_shortcode($atts) {
global $total;
$totalNew = $total[$atts['name']];
$total[$atts['name']] = 0;
return $totalNew;
}
add_shortcode('show-total', 'show_total_shortcode');
Can you show me how I can modify this to factor in the quantity of each item into the total?
Thanks!
David