Skip Navigation

[Waiting for user feedback] Calculating total from loop of quantities multiplied by costs

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 1 voice.

Last updated by Minesh 10 hours, 4 minutes ago.

Assisted by: Minesh.

Author
Posts
#2795626
Screenshot 2025-02-06 at 3.41.39 PM.png

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

#2795668

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please tell me where you added the custom shortcode that calculate the total as well as send me problem URL and admin access details. Also, plese tell me what view you are using to display and make the total.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.