Hi,
I am trying to calculate the sum of the values of several custom fields, but if I'm getting a "failed update" error, I also often get a critical error on the Content Template that forces me to rebuild it from scratch . It seems to work only if the elements are only 2.
The code I'm using is:
[wpv-calculate] [types field='quality-management-system' output='raw'][/types] + [types field='price' output='raw'][/types] + [types field='time-of-payment' output='raw'][/types] + [types field='delivery-time' output='raw'][/types] + [types field='support-documents' output='raw'][/types] + [types field='non-conformities' output='raw'][/types] + [types field='availability-collaboration' output='raw'][/types] [/wpv-calculate]
I have checked all my fields and:
- every option has a numeric value,
- the fields are mandatory, so there is no page with one or more of these fields empty.
I have added this code to my file functions.php:
add_shortcode('wpv-calculate', 'calculate_shortcode');
function calculate_shortcode($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
return round($content);
}
Can you please help me make this calculation work?
Thanks,
Nicola
Hi Nicola,
Thank you for contacting us and I'd be happy to assist.
In my experience, using a generic calculate shortcode that uses the 'eval' function becomes problematic. There is no way to add checks for empty or '0' values, in this approach.
A better approach for calculations like these is to have an independent shortcode that gets the target custom field values, does the necessary processing and calculations on them, and then returns the resulting value.
Here is an example of a custom shortcode such as this, from another forum thread:
https://toolset.com/forums/topic/calculation-shortcode-issue/#post-2695371
I hope this helps and please let me know if you need further assistance.
regards,
Waqar
Hi Waqar,
thank you for your reply. I've been able to address the issue creating a view vith my [wpv-calculate] code. Embedding this view in my Custom Template is working just fine. Yesterday my issues happened when I was trying to use this code directly in the Custom Template.
I'll still take a look at the code you posted, but I think my solution will be okay (unless you advise otherwise). This is because all the custom fields used in my calculation are mandatory, and they all contain numeric values greater than 0.
Thanks,
Nicola
Hi Waqar,
I marked the ticket as resolved since I addressed the issue with my view. However, I’ve reopened it briefly to allow you to respond to my last question. Please let me know if you recommend anyway the shortcode approach or if my solution is adequate for the conditions described.
Thanks for your guidance.
Best,
Nicola
Thanks for the update and glad that it is working now.
> This is because all the custom fields used in my calculation are mandatory, and they all contain numeric values greater than 0.
- With these two conditions taken care of, I feel it should be safe to continue using the 'wpv-calculate' shortcode approach.
Thanks a lot for the clarification!
Best regards,
Nicola