Skip Navigation

[Resolved] Error with wpv-calculate when there are more than 2 values

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Waqar 6 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2696299

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

#2696353

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

#2696390

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

#2696399

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

#2696401

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.

#2696407

Thanks a lot for the clarification!

Best regards,
Nicola