Difference between revisions of "How to add custom placeholders to email templates"

From aMember Pro Documentation
Jump to: navigation, search
Line 1: Line 1:
 
Add this code to [[site.php file]]:
 
Add this code to [[site.php file]]:
 
<source>
 
<source>
<?php
 
 
Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, 'mailTemplateParse');
 
Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, 'mailTemplateParse');
 
function mailTemplateParse(Am_Event $e){
 
function mailTemplateParse(Am_Event $e){
Line 25: Line 24:
 
For example if you want to add product name and expiration date to send_payment_mail template use this code:  
 
For example if you want to add product name and expiration date to send_payment_mail template use this code:  
 
<source>
 
<source>
<?php
 
 
Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, 'mailTemplateParse');
 
Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, 'mailTemplateParse');
  

Revision as of 03:08, 3 November 2015

Add this code to site.php file:

Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, 'mailTemplateParse');
function mailTemplateParse(Am_Event $e){
    $tmpl = $e->getTemplate();
    $tmpl->setPlaceholder("TEST");
}

Now you can use this placeholder in templates: %placeholder% it will be replaced automatically to "TEST"

Inside mailTemplateParse you can access variables which are assigned in template already. For example, if template supports %user.% or %invoice.% placeholders you can access both User and Invoice objects:

/**
 * @var User $user;
 * @var Invoice $invoice;
 */
$user  = $tmpl->user;
$invoice  = $tmpl->invoice;


For example if you want to add product name and expiration date to send_payment_mail template use this code:

Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, 'mailTemplateParse');
 
function mailTemplateParse(Am_Event $e){
    $tmpl = $e->getTemplate();
    $config = $tmpl->getConfig();
    if($config['name'] != 'send_payment_mail') return; // Do not add any placeholders  to another template.        
 
    // Get payment record from email template; 
    $payment = $tmpl->payment;
 
    // Find corresponding access records for that payment.
    $access = Am_Di::getInstance()->accessTable->findBy(array('invoice_payment_id' => $payment->pk()));
 
 
    if(!$access) return; // No access was added, something is wrong.
 
    // Load titles of all products purchased. 
    $products = array();
    foreach($access as $a){
        $products[]  = Am_Di::getInstance()->productTable->load($a->product_id)->title;
    }
 
    $tmpl->setProducts(join(', ', $products));
    // All access records will have the same expiration date so we will take expiration date from first record. 
    $tmpl->setExpiredate(amDate($access[0]->expire_date));
}


Now you can use these two placeholders %products% %expiredate% in Send Payment Email template.