Skip Navigation

[Resolved] Calculate % from two custom Toolset fields

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

Problem:

How to retrieve two custom field items and show the percentage of one with another?

Solution:

Create a custom shortcode and use that shortcode inside your content.

add_shortcode( 'calculate', 'calculate_func' );
function calculate_func( $atts ) {
  
    $goal = do_shortcode("[types field='field1'][/types]");
    $raised = do_shortcode("[types field='field2'][/types]");
 
    $result = (100 * $raised) / $goal;
     
    return round($result, 2);
}

Replace field1 and field2 with the slug of your fields.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/

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

This topic contains 9 replies, has 2 voices.

Last updated by Jean Rapenne 2 years, 2 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2547835

Hi,

I want to calculate a ù using 2 number custom toolset fields.

- A target amount
- A raised amount

Need to get the % of raised on target.

Could you explain to me the way to achiev. I need to have the result displayed 2 ways (0.n and nn.nn%)

Regards.

Jean

#2548297

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

I will try to give the gist of what you need to do.

You will need to create a custom shortcode:

https://toolset.com/documentation/programmer-reference/adding-custom-code/how-to-create-a-custom-shortcode/

Inside the shortcode, you need to use PHP code to do the calculations and use that shortcode as the end result inside your view.

This can give you a starting point:

https://toolset.com/forums/topic/calculating-types-custom-fields-and-display-in-html-container/#post-345125

Thank you.

#2548337
2Fields-Goal-Reached.jpg

Hi Chris !

I'm sorry, I'm not an enough skilled developer to achieve to a solution. and I need a more step by step detailed tutorial, to achieve.

If you want to have a look to my Toolset config, please find the above temporary admin access link:

hidden link

Please find the attached capture of the 2 fields that will be the basis of calculating.

Regards.

Jean

#2549603

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

According to your fields you need to add something like this:

add_shortcode( 'calculate', 'calculate_func' );
function calculate_func( $atts ) {

	$goal = do_shortcode("[types field='montant-objectif-final-levee-de-fonds'][/types]");
	$raised = do_shortcode("[types field='montant-final-deja-leve'][/types]");

	return (100 * $raised) / $goal;
}

I did not test the code and it is just a starting point. You will be able to add the [calculate] shortcode inside the view that you want to show the result.

Also the (100 * $raised) / $goal part is a sample you need to add whatever calculation you have. The goal and raised are the ones you annotated in the screenshot.

I already explained how to add a custom code in Toolset giving the proper link to the documentation.

Please have a complete backup of the website before adding any custom code.

We do out best to help get you started but we can not give the whole working code as it is not part of our support scope.

You are welcome to hire a developer for further investigation:

https://toolset.com/contractors/

Thanks.

#2549657

Hi Christopher,

Many thanks for your help

I've created a Snippet in the Snippet plugin with the code you give to me (with their new shorter tags)

I'm blocked now, because I don't know now what to do to create the [calculate]shortcode that have to be included in my Elementor widgets.

Regards.

Jean

#2550035

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

You need to add the shortcode using either the TEXT or SHORTCODE widget of Elementor;.

Inside that just add:

[calculate]

Please consider that the value of a custom field differs per each post. That is why you need to add the shortcode in either a Toolset View or the Elementor Template that you added for the Single Post item.

Thanks.

#2550069

My issue is resolved now. Many thanks to you!

Jean

#2550117

Hi,

I've just tried to integrate the [calculate] shortcode and achieve to have the percent displayed.

I have a rounding issue, the field displays: 71.328352867165 and I need only display 2 decimals after the comma (71.33, in this case).

Could you give to me the right code to achieve.

Regards.

Jean

#2550121

Christopher Amirian
Supporter

Languages: English (English )

Hi Jean,

Just to make sure we are on the same page, when you set a ticket as resolved you will need to open up another ticket.

You can use the "round" function in PHP to achieve that:

hidden link

So the code changes to:

add_shortcode( 'calculate', 'calculate_func' );
function calculate_func( $atts ) {
 
    $goal = do_shortcode("[types field='montant-objectif-final-levee-de-fonds'][/types]");
    $raised = do_shortcode("[types field='montant-final-deja-leve'][/types]");

    $result = (100 * $raised) / $goal;
    
    return round($result, 2);
}

Thanks.

#2550151

Very nice support!
My issue is resolved now.
Thank you!