Skip Navigation

[Resolved] Calculate with Toolset

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

Problem:
Can we calculate Fields values wit Toolset?
If so, how?

Solution:
It's not supported natively, but you can reach a few ideas here:
https://toolset.com/forums/topic/count-of-custom-field-if-it-has-an-entry/#post-608189

This support ticket is created 6 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.

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 1 reply, has 2 voices.

Last updated by Beda 6 years, 10 months ago.

Assisted by: Beda.

Author
Posts
#607957

Tell us what you are trying to do?
I have a CPT called nominations
Attached to this is a field called votes - slug nomination-vote
For each judge I want to create a count of the number of votes they have awarded.

What I'm trying to achieve is a query that sees if a custom field has an entry. If it does to then count the entry once. To then count all times that field has a value across all nominations for a given judge. (I understand how to do the view for this but not the shortcode for the custom field count)

Is there a similar example that we can see?
https://toolset.com/forums/topic/counting-values-in-custom-field/
https://toolset.com/forums/topic/sum-of-fields-in-a-view-2/

#608189

I am not sure to understand your goal:
- do you want to perform mathematical operations with numeric values, which are stored in a Custom Field?
- or do you want to count how many instances of a certain Field exist and are filled out with a value across a Post Type?
- or do you want to do both? ?

Basically, Toolset is not the tool to perform any form of calculations.
A little restricted exception provides HTML conditional in which you can make basic calculations to compare then the value to something else:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/#using-fields-with-math-operators

But this is only possible within a condition.

Now, to perform mathematical operations with numeric values, which are stored in a Custom Field you can use this steps as reference:
- Register the custom shortcode "calculate":

/**
 * Add custom shortcode to perform calculations with Types fields
 * 
 * attribute 'round' for decimal places, defaults to zero
 */
add_shortcode('calculate', function( $atts = [], $content=null ) {

	$atts = shortcode_atts([ 'round' => 0 ], $atts );

	$content = strip_tags( wpv_do_shortcode($content) );
	$content = eval("return $content;");
	return round($content, $atts['round']);
});

- Then use it like this:

[calculate round=2][types field="weight"]*[types field="unit-price"]/100[/calculate]

Or, another method:
- Register this 2 ShortCode:

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, Register those ShortCodes in Views > Settings > Compatibility > 3rd Party ShortCodes

- Use the ShortCodes in a View as this:

<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]

Instead, to calculate how many instances of a field are there, you can use a simple count() on the get_post_meta() of the field in question.

This is custom code that we cannot assist fully.

If you require add-on functionality to Toolset with Custom Code, you may be interested in the Contractors List:
https://toolset.com/contractors/