[Resolved] Need to find Sum total of fields in child loop

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.

This topic contains 5 replies, has 2 voices.

Last updated by Adriano 5 years, 10 months ago.

Assigned support staff: Adriano.

Author
Posts
#219220

Looking for a way to find the totals of a certain field.
Here is my scenario, I have a post type call Riders and a post type called Donations. The donations post type is a child of the Rider Post type.
Then I have set up a child view to pull donations set by parent view. Then I have a parent view that just lists riders and pulls in the child donations view in the loop.

I need to list all donations per rider and then display a total of the donations that belong to that specific rider.

Here is what I have in my functions file now to calculate the total but it seems like I need to specify a rider id or something?

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

It does total currently but it is totaling every instance of the field instead of per rider. View the link below to see what I mean

hidden link

Any help would be great, I am very close I think.

#219247

Adriano
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Dear Brent,

Try to replace your code with this below:

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

Please let me know if you are satisfied with my answer and if I can help you with any other question which is related with this one.

#219269

Hi Adriano,

Thanks for the quick response but it's still not quite right.
hidden link

I need it to total each riders donations. Maybe I have my view setup wrong?
This is what I have in my child view.

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<ol>
<wpv-loop>

  • Donor: [types field="donor-first-name" id=""][/types] [types field="donor-last-name" id=""][/types]<br/>
    Amount: [types field="donation-1" id=""][/types]
    [add-to-total][types field="donation-1" id=""][/types][/add-to-total]<br/>
    <br/>
  • </wpv-loop></ol>
    Total: $
    [show-total]
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
    [wpml-string context="wpv-views"]No donations yet.[/wpml-string]
    [/wpv-no-items-found]
    [wpv-layout-end]

    Any suggestions?

    #219276

    Adriano
    Supporter

    Languages: English (English )

    Timezone: America/Sao_Paulo (GMT-03:00)

    Dear Brent,

    Let's try this one:

    global $total;
    add_shortcode('add-to-total', 'add_total_shortcode');
    function add_total_shortcode($atts, $content = '') {
    global $total;
    $string = str_replace('$', '', $string);
    $total += wpv_do_shortcode($content);
    }
    add_shortcode('show-total', 'show_total_shortcode');
    function show_total_shortcode() {
    global $total;
    $totalNew = $total;
    $total = 0;
    return $totalNew;
    }
    
    #219279

    That did it! You Rock! Thanks so much for this, I was racking my brain. Your the Man!
    hidden link

    #219280

    Adriano
    Supporter

    Languages: English (English )

    Timezone: America/Sao_Paulo (GMT-03:00)

    I only gave you one line, you did the whole code 😀

    You are welcome.