Skip Navigation

[Resolved] Sum of field part II

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

Our next available supporter will start replying to tickets in about 1.86 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by nicolaS-3 4 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#1536235

Hi,
I am trying to apply the solution given by Ana long time ago to sum one of my fields,
https://toolset.com/forums/topic/sum-of-fields/
unfortunately I get no results, nothing is displayed. Can you tell me if the function works actually ?
Thanks
Regards
Nicola

#1536263

If you want to calculate the sum of some custom field in a View, here is a straightforward solution. First, save these two shortcodes in your child theme's functions.php file, or in new custom code snippets in Toolset > Settings > Custom Code:

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

Then you can use these shortcodes in a View to add up the total of some custom field in a View:

<wpv-loop>
    ... your loop content goes here...
    [add-to-total]
      [types field="your-field-slug" output="raw"][/types]
    [/add-to-total]
</wpv-loop>

Display sum of all fields outside the loop: [show-total]
#1536473

Hi Christian,
thanks for this, the calculation works like a charm ! the only "detail" is that I need to apply the [format_money] format to the total for getting the EU format (thousands separator = dot, decimal separator = comma + 2 decimals), but it doesn't work .... any clue ?
thanks
regards
Nicola

#1537781

I don't know anything about format_money, is that another custom shortcode?

#1538441

Hi
yes, its a shortcode suggested on your support site that displays numbers as money in EU format. Here is the code:

//This function creates a short code to format money
function yls_format_money( $atts, $content ) {
    $content = do_shortcode($content);
    return $value = number_format($content, 2, ',', '.');
}
add_shortcode('format_money', 'yls_format_money');

It works well on fields, but not on the totals.
Regards
Nicola

#1538971

I see, that results in some nested do_shortcode calls that are probably causing the problem. I think I would try to avoid that nesting of shortcodes by rewriting the show_total_shortcode function to accept a new shortcode attribute like format. If the shortcode attribute has a value of "money_eu", the updated code should apply the same number_format as in the yls_format_money function. Here's some updated code for the show-total shortcode. Please replace what I shared above in the second code block with this updated code:

function show_total_shortcode( $atts ) {
  global $total;
  $atts = shortcode_atts( array(
    'format' => ''
  ), $atts );
  $totalNew = $total;
  $total = 0;
  if ( $atts['format'] == 'money_eu' ) {
    $value = number_format($totalNew, 2, ',', '.');
    return $value;
  }
  return $totalNew;
}
  
add_shortcode('show-total', 'show_total_shortcode');

Then to display the total, update the show-total shortcode to include the money_eu format attribute:

Display sum of all fields outside the loop: [show-total format="money_eu"][/show-total]

You can still use the show-total shortcode without the format option if you want to display a non-money number elsewhere:

[show-total][/show-total]

Let me know what you think.

#1539421

Hi Christian,
thanks, now is perfect, I am sure it will be useful to all EU guys !
Kind regards
Nicola