Being able to group Thank You Letters by contact is a great feature for both everyday receipts (we have a company that will donate via multiple smaller donations at once) and end of the year receipts.

But I'm running into a problem, if I use an if statement around a contribution token, the token no longer loops for each contribution printed. For example:

{crmAPI var='contresult' entity='Contribution' action='get' sequential=0 id=$note_id}
 {foreach from=$contresult.values item=contribution}
  {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}

The function doesn't display anything when I 'group by' (If I run a thank you letter for a single contribution it does work.)

In this example:

{capture assign='note_id'}{contribution.contribution_id}{/capture}
{crmAPI var='result' entity='Note' action='get' sequential=0 entity_table="civicrm_contribution" entity_id=$note_id}
 {assign var='note_count' value=$result.count}
  {if !$note_count}None{elseif $note_count}
   {foreach from=$result.values item=note}{$note.note}{/foreach}

It only returns "None" once, when the contributions all have notes.

How can I make the if statement run for each of the contributions selected for the thank you letter?

  • Can you edit your question and post the code, I guess really the HTML, you're using? That way folks can see the logic, loops, foreach, etc.
    – Paul-Tahoe
    Jan 26, 2016 at 23:33
  • @Paul-Tahoe Done!
    – RayWright
    Jan 27, 2016 at 16:36
  • Have you looked at this github.com/civicrm/civicrm-user-guide/blob/master/docs/… ? Near the bottom of the page is some html/smarty's code. It isn't as complex as yours though it does fill out a table with all the different donations for the period. Also, it appears that the thank you letter already has an array that most likely includes the non_deductible_amount so you might be able to do like the example and stick you if then in their inner loop.
    – Paul-Tahoe
    Jan 27, 2016 at 18:25

1 Answer 1


The error comes from {capture assign='note_id'}{contribution.contribution_id}{/capture}. When grouping, if you have more than one contribution the output will be ie. 1, 2, 3, 4 ..., try printing {$note_id} when grouping and you'll see what I mean, passing 1, 2, 3, 4 as contribution_id to the API call the response is "is_error": 1

Edit: so having a look at CRM/Contribute/Form/Task/PDFLetterCommon.php, I believe that's the correct class, there´s a $contributions variable which holds the contributions, so if you loop through it, the above could look like this:

{foreach from=$contributions item=contribution}
{crmAPI var='result' entity='Note' action='get' entity_table="civicrm_contribution" entity_id=$contribution.id}
 {assign var='note_count' value=$result.count}
  Contribution note: {if !$note_count}None
  {elseif $note_count}
    {foreach from=$result.values item=note}
 | Non deductible amount: {if $contribution.non_deductible_amount gt 0}${$contribution.non_deductible_amount}{else}N/A{/if}

Now this works when grouping but it won't when you are not grouping, so my guess is that you'll have to create two ThankYou templates, one for grouping and one for no grouping.

Also important to mention this is smarty they are not custom tokens.

  • My apologies, my original post I referenced both smarty and custom tokens. (I also tried writing a custom token for the note variable but it also failed to show a variable for multiple contributions). I since edited it and added code so I pulled out the mention of custom tokens for an easier read. I printed {$note_id} and it printed out the contribution IDs.
    – RayWright
    Jan 27, 2016 at 20:41
  • Updated the answer, hope that helps.
    – Andrei
    Jan 27, 2016 at 22:48
  • you are my hero! Totally works didn't realize I could put the foreach around the API.....mind blown!
    – RayWright
    Jan 27, 2016 at 23:52
  • Glad it helped!
    – Andrei
    Jan 27, 2016 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.