Skip Navigation

Need to find Sum total of fields in child loop

This support ticket is created hace 8 años, 4 meses. 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 5 respuestas, has 2 mensajes.

Last updated by Adriano hace 8 años, 4 meses.

Assigned support staff: Adriano.

Autor
Mensajes
#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

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

    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

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

    You are welcome.