Is there a variable that one can use in smarty in a message template to simply determine if a contact has an active membership?
2 Answers
Don't think you have a direct variable to use in the template. Worth trying an extension to provide this? It can be done by simply writing something like below in your hook.
function yourextension_civicrm_postProcess($formName, &$form) {
if ($formName == "your_form") {
//Check if user has an active membership of type "General".
$activeMembership = civicrm_api3('Membership', 'getcount', array(
'contact_id' => <contact_id in $form>,
'active_only' => 1,
'membership_type_id' => "General",
));
//Assign a new variable to the template.
$form->assign('activeMembership', $activeMembership);
}
}
You can now use this variable as {$activeMembership}
in the template to check if user has a membership of the req type.
You can use CiviCRM api through smarty to retrieve if membership is active for a contact. You can achieve this by
- Turn on CIVICRM_MAIL_SMARTY (set to TRUE or 1) in civicrm.settings.php
- add below api snippet in your message template
($result will hold result of api call)
{capture assign=contactId}{contact.contact_id}{/capture}
{crmAPI var='result' entity='Membership' action='getcount' sequential=0 contact_id=$contactId membership_type_id="General" active_only=1}
{if $result gt 0}
'active membership'
{else}
'not active'
{/if}
HTH
Pradeep