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.
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.
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?
That did it! You Rock! Thanks so much for this, I was racking my brain. Your the Man!
hidden link
I only gave you one line, you did the whole code 😀
You are welcome.