Skip Navigation

[Resolved] Summerize scores from field value

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

This topic contains 2 replies, has 1 voice.

Last updated by patrikK-2 3 years, 5 months ago.

Author
Posts
#2088281
score.jpg
front2.jpg
front1.jpg

This question is related to another post you helped me in.
https://toolset.com/forums/topic/repeated-group-fields-not-showing-up-in-toolset-blocks/#post-2088277

I have a repeatable fields "Fish scores"
and a Single line field.

Is there any solution, in toolset, that i can use to sum the score on to the singel field, if not, is there any solution to do it on front end ?
I used the guide you showed me in the other ticket with repeatable fields.

Score.jpg shows the backend.
I manually add the score in total score. but i would like the Fish measure is counted from every added fishes to the singel field.
Or in front end: front2jpg

front1.jpg shows the block editor how i do the leader board layout.

AND
just to show that i have tried my self, with no luck thoug. i followed this*:;
https://toolset.com/forums/topic/sum-of-fields/

#2088323

What i have tried is this but i have no luck with this...

In the view that shows all repeatable field added the shortcode Ana describes in the ticket sum-of-fields
and i have added the code to function.php

this is how my loop looks like

[wpv-layout-start]
	[wpv-items-found] 
	<!-- wpv-loop-start -->
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-rfg-fishes1"]
     Count: [count_numeric_cf field="fishmeasure" action="add"]
	</wpv-loop>
	<!-- wpv-loop-end -->

	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]

Sum:[count_numeric_cf field="fishmeasure" action="sum" format_decimals="2"]
[wpv-layout-end]

and PHP:

function count_numeric_cf_func ($atts ) {
    extract( shortcode_atts( array(
        'action' => 'sum',
        'field' => '',
        'format_decimals' => 2,
    ), $atts ) );
    $output = '';
    global $post, $wpdb, $wp_query;
    static $sum;
    switch ($action) {
        case 'add':
            if (!empty($field) && isset($post->ID)) {
                $value = get_post_meta( $post->ID, $field, true);
                if (is_numeric($value)) {
                    if (!isset($sum[$field])) {
                        $sum[$field] = 0;
                    }
                    $sum[$field] += $value;
                }
            }
            break;
        case 'sum':
            if (!empty($field) && isset($sum[$field])) {
                $output = number_format($sum[$field], $format_decimals);
            }
            break;
    }
    return $output;
}
add_shortcode( 'count_numeric_cf', 'count_numeric_cf_func' );
#2088345

After i found the solution you already written in a different ticket i solved it on my own! It was pritty easy then. but to find the correct ticket, it was quite hard 🙂

https://toolset.com/forums/topic/sum-of-field-part-ii/