This thread is resolved. Here is a description of the problem and solution.
Problem: I would like to calculate values from custom fields, and I would like to reset the calculated total at a specific point during loop execution.
Solution: Use the add-to-total, show-total, and reset-total shortcodes provided in another ticket. Use wpv-do-shortcode instead of do-shortcode to prevent some calculation problems.
This support ticket is created 6 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.
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.
I have a View where I am looping through a post type and adding the contents of a field to get a total.
I am using [add-to-total] for this. And at the end of the loop I put [show-total]
This works on the first loop, but then I go back to a nested view and come back to loop through the same CPT again with a different filter, and use [add-to-total] again. But this time, the value from the previous loop is still stored so it adds together and I get a larger number than I should.
For example, I loop through a CPT for a particular musician and add together the "Number of Services" for that musician and get 4
Then I loop through again for a different musician and the answer should be 24, but because 4 was already stored, my [show-total] shows 28 instead of 24. And then the next time, 28 is stored and added to the next loop, and etc.
Is there a way to zero out the total at the beginning of each nested loop?
These shortcodes, add-to-total and show-total, are not Toolset shortcodes.
They are custom code, and I'm guessing you may even have been given the code for them in our support forums, but I'd need to see the code for them to be able to help.
Could you please paste the code for them here so I can see?
Then also show me in your View where you are using them.
Yep - that's true. It is custom code, and code that I got from Toolset support.
Here is the code I'm using in functions.php:
// to allow math calculations in views
global $total;
$total = 0;
add_shortcode('add-to-total', 'add_total_shortcode');
function add_total_shortcode($atts, $content = '') {
global $total;
$total += do_shortcode($content);
}
add_shortcode('show-total', 'show_total_shortcode');
function show_total_shortcode() {
global $total;
return $total;
}
// to allow math calculations on multiple shortcodes
add_shortcode('wpv-shortcodecalculate', 'calculate_shortcode2');
function calculate_shortcode2($atts,$content=null) {
$content = wpv_do_shortcode($content);
$content = eval("return $content;");
return round($content);
}
And here is the view that I'm using:
<!-- wpv-loop-start -->
<table>
<tr><td>SERVICES OFFERED</td><td># of Services</td><td>ACCEPT or DECLINE</td><td> </td><td></td></tr>
<wpv-loop>
[wpv-conditional if="('[types field='concert-name' id='@booking.parent'][/types]' eq 'WEST SIDE STORY: THE GENIUS OF BERNSTEIN' )"]
[add-to-total]4[/add-to-total]
[wpv-view name="concert-list-for-contract" ids="164"]
[/wpv-conditional]
[wpv-conditional if="('[types field='concert-name' id='@booking.parent'][/types]' eq 'A CHRISTMAS CAROL: THE CONCERT' )"]
[add-to-total]4[/add-to-total]
[wpv-view name="concert-list-for-contract" ids="165"]
[/wpv-conditional]
[wpv-conditional if="('[types field='concert-name' id='@booking.parent'][/types]' eq 'THE MAGIC OF MOZART' )"]
[add-to-total]4[/add-to-total]
[wpv-view name="concert-list-for-contract" ids="166"]
[/wpv-conditional]
[wpv-conditional if="('[types field='concert-name' id='@booking.parent'][/types]' eq 'THE ROMANCE OF SCHUMANN' )"]
[add-to-total]4[/add-to-total]
[wpv-view name="concert-list-for-contract" ids="167"]
[/wpv-conditional]
[wpv-conditional if="('[types field='concert-name' id='@booking.parent'][/types]' eq 'HEAVENLY VOICES' )"]
[add-to-total]4[/add-to-total]
[wpv-view name="concert-list-for-contract" ids="168"][/wpv-conditional]
[wpv-conditional if="('[types field='concert-name' id='@booking.parent'][/types]' eq 'THE GLORIOUS GIFT OF BIRDS' )"]
[add-to-total]4[/add-to-total]
[wpv-view name="concert-list-for-contract" ids="169"]
[/wpv-conditional]
</wpv-loop>
<tr><td>Total services offered</td><td>[show-total]</td><td> </td><td> </td><td> </td></tr>
</table>
This works for the first loop - it adds together the concerts that meet the condition and comes up with a total of 8 - but then when it goes through the loop the next time, it ADDS that 8 to the next total and keeps on doing that.
I tried this code at the end of the loop to zero out the total:
Then where you insert the show-total shortcode you could insert the reset-total shortcode immediately afterwards so that it is reset for the next View.
Now I need help with one more related thing - in the code I sent you, I was simply adding a number to the total in each loop.
What I really want to do is to add the value of a numeric custom field - but that doesn't seem to be working.
The custom field is called "services" and it belongs to a custom post in a relationship with the current post in the loop. The current post in the loop is in an intermediary post type called "Bookings Intermediary Posts". The numeric field I want to use is in the related post type "Concerts".
When I inserted this code, I chose the related custom post type "Concerts" in the wizard.
But this doesn't work - it doesn't add anything to the total.
I have used this code in a different site on a numeric field in the same custom post type as the current post in the loop and it works fine - so I imagine the issue has something to do with identifying where to get the custom field.
Hi, Nigel is out this week so I'll try to help out. Can you copy the Types field shortcode and paste it outside the addition shortcode? Let me know what this shows on the front-end of the site:
May I log in and see how you have this View set up? Please provide a URL where I can see the View on the front-end of the site. I will activate private reply fields here so you can share credentials securely.
I'm not really sure what's going on here. As a test, can you try adding this line to your wp-config.php file? This option can help fix some issues related to complex conditional content and regex evaluation on the server:
ini_set('pcre.jit', false);
If this doesn't help, I think it might be time to re-evaluate this display. You can see based on performance and management, this type of application and mathematical calculation approach has reached its breaking point. These calculation shortcodes work best in very simple applications and aren't designed to build spreadsheets or complex applications, or to be split apart into multiple Views.
Similarly, nested Views inside of nested Views inside of nested Views will result in un-optimized queries. A temporary patch here is to apply pagination to these results so the initial load times are shorter. Another patch is to apply a caching plugin, but that won't help when you're doing development and need to update the results quickly. A better solution is to figure out an automated way to simplify this View structure so that you don't need so many queries. That could mean you calculate values when posts are saved, then apply those values directly to other posts as custom fields, reducing the number of queries necessary to display the values.
I tried adding the code to my wp-config.php, but it didn't help.
Thanks for all the thoughts on the nested views ... I'm afraid I wouldn't know how to go about calculating values and storing them when the post is made - and I don't know how that would work because then if the post changes, it would have to change the value in every post it was saved to in other CPTs, right? I don't think I understand how that work. For example, if I assign 50 musicians to a concert that has 4 services and somehow store that number 4 in their Musician CPT, then what happens when the number of services for that concert changes - which could happen? I could try pagination - but the problem is that I am trying to create a spreadsheet that can be put into Excel so to have it on multiple pages would make that a difficult process.
I wish you would write mathetmatical approaches that ARE designed for spreadsheets - after all, one of the custom fields available is Numeric, and I don't know what the point of that is if you can't do math on the field! It would be AWESOME if Toolset would work on making it native to do math on numeric fields.
By the way - I created a really simple view that uses the same math shortcodes - just one loop of one CPT, no nested views, and it STILL doesn't work.
You can check out this page:
hidden link
which is using this view:
hidden link
Since the view is so simple, I must be doing something else wrong with those shortcodes...