Skip Navigation

[Resolved] Code for calculating sum of another group of fields

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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by JamesS2731 11 months, 2 weeks ago.

Assisted by: Luo Yang.

Author
Posts
#2607655

Hi there,

I've found some code in one of your old support threads to calculate the sum of fields wrapped in [add-to-total][/add-to-total] but I wondered how to duplicate and alter the code to allow me to calculate the total of a separate group of fields (in this case, the first group is the total of the cost out, I'd like a separate total of cost in fields)

Original thread - https://toolset.com/forums/topic/custom-fields-calculations-sum-total-of-two-fields/

I presumed that I could just take this:

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');

and amend it to add a '2' after the shortcode names to give:

global $total;
function add_total_shortcode_2($atts, $content = '') {
global $total;

$total += wpv_do_shortcode($content);
}

add_shortcode('add-to-total-2', 'add_total_shortcode_2');

function show_total_shortcode_2() {
global $total;
$totalNew = $total;
$total = 0;
return $totalNew;
}

add_shortcode('show-total-2', 'show_total_shortcode_2');

And then use [add-to-event-2] and [show-total-2] as the shortcodes, but that didn't work.

Any pointers would be most appreciated.

Best regards
James

#2607779

Hello,

It is a custom codes issue, here is a sandbox website:
Login URL: hidden link

You can reproduce the same problem in above sandbox website, I need a live website to test and debug the custom PHP codes, thanks

#2607841

Hi Luo,

Many thanks for your quick response.

I've given you access to the live site (see below). It's a dev site and has been backed up so feel free to check whatever needed.

I've left the second code snippet in the Custom Code section but have deactivated it. However, I removed the two shortcodes [add-to-total-2] and [show-total-2] from the activity view.

URL: hidden link
UN: toolset
PW: xuBfPT^B9v@jPs

Activity View: hidden link

Event page: hidden link

Any thoughts or ideas most welcomed.

Kind regards
James

#2608071

Thanks for the details, I have done below modifications in your website:
1) modify the custom code snippet as below "calculator-sum":
hidden link

global $total;
function add_total_shortcode($atts, $content = '') {
global $total;
$total[$atts['name']] += wpv_do_shortcode($content);
}
  
add_shortcode('add-to-total', 'add_total_shortcode');
  
function show_total_shortcode($atts) {
global $total;
$totalNew = $total[$atts['name']];
$total[$atts['name']] = 0;
return $totalNew;
}
  
add_shortcode('show-total', 'show_total_shortcode');

2) Edit the post view:
hidden link
In section "Loop item in Activities View", change the shortcodes from:
[add-to-total][types field='activity-price-out' format='FIELD_VALUE' item='@activity-event.parent'][/types][/add-to-total]

To:
[add-to-total name='activity-price-out'][types field='activity-price-out' format='FIELD_VALUE' item='@activity-event.parent'][/types][/add-to-total]

In section "Loop Editor", display the result like this:
[show-total name='activity-price-out'][/show-total]

Test it in frontend:
hidden link

It works fine, for your reference.

#2608309

That's perfect! I can then use that for other columns and change the name for each. Brilliant.

Many thansk for your help as always.

Have a great weekend!

James

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.