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
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.
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
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.
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
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.
My issue is resolved now. Many thanks to you!
Jean
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
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.
Very nice support!
My issue is resolved now.
Thank you!