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
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]
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
I don't know anything about format_money, is that another custom shortcode?
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
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.
Hi Christian,
thanks, now is perfect, I am sure it will be useful to all EU guys !
Kind regards
Nicola