Skip Navigation

[Resolved] Is there any way to calculate sum of values of particular field in picked items?

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to be able to calculate the total sum of the custom field values in a View.

Solution: There's nothing built-in to Toolset that will perform summations like this, it requires custom code. This solution is only for simple cases. Add to your child theme's functions.php file:

global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
  
$total += wpv_do_shortcode($content);
}
  
add_shortcode('add-to-total', 'add_total_shortcode');
 
function show_total_shortcode() {
global $total;
$totalNew = $total;
$total = 0;
return $totalNew;
}
  
add_shortcode('show-total', 'show_total_shortcode');

Then in the View's Loop, duplicate the custom field shortcode and wrap it in the shortcode add-to-total, and use the show-total shortcode to display the total value:

<wpv-loop>
    [types field="your-numeric-field" output="raw"][/types]
    [add-to-total]
      [types field="your-numeric-field" output="raw"][/types]
    [/add-to-total]
</wpv-loop>
[show-total]

Tis is a simple solution that only works in one column of a View, and only works with a simple numeric field. More complex cases will require your own custom code modifications.

Relevant Documentation:
https://toolset.com/forums/topic/nested-view-with-conditional-sums/

This support ticket is created 5 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by antonM-7 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1310681

Hi!

Please tell, is there any way to calculate sum of values of prticular field in all items, picked by query in Views?

For example, a custom type "Event" has a field "Num-of-visitors", and i want to get a sum of all visitors that took part in all events up to particular date to show this number in summary. Is it possible?

#1310705

Hi, there's nothing built-in to Toolset that will accomplish this. It requires custom code. In simple cases you can use the following custom code to add up the values of some custom field in a View of posts.

Add to your child theme's functions.php file:

global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
 
$total += wpv_do_shortcode($content);
}
 
add_shortcode('add-to-total', 'add_total_shortcode');

function show_total_shortcode() {
global $total;
$totalNew = $total;
$total = 0;
return $totalNew;
}
 
add_shortcode('show-total', 'show_total_shortcode');

Then in the View's Loop, duplicate the custom field shortcode and wrap it in the shortcode add-to-total, and use the show-total shortcode to display the total value:

<wpv-loop>
    [types field="your-numeric-field" output="raw"][/types]
    [add-to-total]
      [types field="your-numeric-field" output="raw"][/types]
    [/add-to-total]
</wpv-loop>
[show-total]

Note that this is a simple solution that only works in one column of a View, and only works with a simple numeric field. More complex cases will require your own custom code modifications. See also here https://toolset.com/forums/topic/nested-view-with-conditional-sums/

#1311613

Thanks for help, problem is solved! Also have now a clue how to do more interesting things. I'll dig deeper myself, thank you!