Skip Navigation

[Resolved] Simple calculation field

This support ticket is created 3 years, 10 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.

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)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1657355

Tell us what you are trying to do?
I have a custom post type Clients, each entry has a custom numeric field named annual-turnover.

What I would like to achieve is to have a total sum for the above custom field that I can place at the bottom of the View.
Just to give you an idea:
CLIENT NAME ANNUAL TURNOVER
Client A 100.00
Client B 250.00

And at the bottom a field that shows the sum of all value of the field annual-turnover so in this case £350.00

I tried to read related posts from other users but they were requiring solutions for quite complex calculation, I need a simple solution.
Can you help?
Many thanks
Best wishes

#1657505

Hello, Views has no calculation or spreadsheet functionality built-in, so unfortunately there is no simple solution. The simplest solution available requires you to implement custom shortcodes in your View to add up the numbers in sequence. I can help you implement this solution if you need assistance.

First, you must create two custom shortcodes by adding the following code in your child theme's functions.php file, or by creating a custom code snippet in Toolset > Settings > Custom Code:

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');

[/php]

Then you must register add-to-total and show-total in Toolset > Settings > Front-end Content > Third-party shortcode arguments. In your View's loop, you must use the classic Editor to wrap the shortcode that displays your custom field in the add-to-total shortcode, like so:

£[add-to-total][types field="your-number-field-slug" output="raw"][/types][/add-to-total]

Change your-number-field-slug to match the slug of your custom number field. You can find the slug in the field editor screen. Note that £ is outside the add-to-total shortcode, if you want to display the symbol next to the value in the loop.

Then to display the total value, you must insert the show-total shortcode somewhere outside the View's loop, and include the £ symbol as needed:

£[show-total][/show-total]

Let me know if you have questions about this.

#1659131

Hello Christian,

Thank you so much for providing me detailed instructions on how to set up calculations fields.
I think that Toolset should seriously introduce an easier way to add calculations by the way.

To be honest, I found easier to use the old Views compared to Toolset Blocks, I needed a table to show few columns and a total calculation and the loop wizard + some hand coding by myself allowed to create the table I wanted.
Just to give you an idea this is wanted I wanted to achieve:
hidden link

Last question how can I get in the total field the commas for thousand value? Eg. instead of 1000, this one? 1,000.

Look forward to your reply
Best wishes

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/format-number-with-commas/

#1662115

I have split your formatting question into another ticket.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.