Add custom placeholders to email templates
Add this code to site.php file:
Am_Di::getInstance()->hook->add(Am_Event::MAIL_TEMPLATE_BEFORE_PARSE, function (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, function (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.