Magento 1.9 – How to Use Custom Variables in PHTML File for Email

custom-variablemagento-1.9transactional-mail

I have created a Transactional Emails template in magento admin to send multiple coupon codes dynamically for this I have created a template file says email/custom.phtml and it is included in email template.

It is included in email template successfully but I am trying to add {{var code12}}, {{ var code13 }} etc. with multiple time but it is not working.

If I add these in direct email template in magento admin then it is working as I want but I want to create {{var}} also dynamically.

I am using below code for sending email :

$senderName = 'custom';
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

$emailTemplate = Mage::getModel('core/email_template')->loadByCode('Custom Template');;
$emailTemplateVariables = array();
$emailTemplateVariables['name'] = 'name';
$emailTemplateVariables['code12'] = 'AB56FFD';
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$mail = Mage::getModel('core/email')
        ->setToName('name')
        ->setToEmail('test@gmail.com')
        ->setBody($processedTemplate)
        ->setSubject('Coupon code')
        ->setFromEmail($senderEmail)
        ->setFromName($senderName)
        ->setType('html');
$mail->send();

My custom.phtml code is here :

##My shopping cart rule are product specific
<table>
<?php 
$allProducts = array(10,20,30);
foreach($allProducts as $product_id)
{
    $product = Mage::getModel('catalog/product')->load($product_id);
    ?>
    <tr><td>
    <?php
    echo $product->getName();
    echo Mage::getModel('core/variable')->loadByCode('code12')->getValue('text');  // it is not working
    ?>
    {{var code12}}  // it is not working
    </td></tr>
    <?php
}
?>
</table>

And Transactional Emails template code is here :

...
{{block type="core/template"  name="email_custom" template="email/custom.phtml" }}
....

How to add custom variables in phtml file?

Best Answer

pass a array

$emailTemplateVariables['code'] = array('code12'=>'AB56FFD','code13'=>'AB5fd6FFD','code14'=>'AB56dfFFD') ;

you have to pass variable to phtml file

{{block type="core/template"  name="email_custom" template="email/custom.phtml" code=$code}}

in phtml file

$code= $this->getCode();
echo $code['code12'];
Related Topic