Get my custom variable into an email template

The question:

We currently use a plugin for gaining points when ordering items.

I want to display the users points balance inside various emails, now the plugin company gave me the code to echo out the current users balance however I am having a hard time figuring out where I can put this so that the variable will be available for me to use in my email templates.

$pointsBalance = Mage::getModel('rewards/customer')->load($customerId)->getUsablePoints();
echo $pointsBalance[1];

Can anyone advise me how I get a PHP variable like this in the right place so I can use it in my email templates like {{points.balance}}?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

There’s a simple way you can do this. Creating a custom variable for the email can be too much time consuming, so the easiest way to get this result would be using a .phtml file on your transnational email.

Add this to your email HTML:

{{block type='core/template' area='frontend' template='email/custom/mynote.phtml' order=$order}}

Create a file path/to/magento/app/design/frontend/base/default/template/email/custom/mynote.phtml

Now in this file you could do something like this:

   $pointsBalance = Mage::getModel('rewards/customer')->load($customerId)->getUsablePoints();
 echo $pointsBalance[1];

Done 🙂

Method 2

For this you need to first find a controller from which email is triggering. If you want to find a controller then you can get controller path by action of form if it is using any form or else you have to check all controllers of that module which consist code for email sending and add this variable to get a value of this variable on email template.

Some thing like

$emailTemplateVariables = array();
$emailTemplateVariables['points.balance'] = Mage::getModel('rewards/customer')->load($customerId)->getUsablePoints();

$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

After adding above code you can get this value in template.

for more details refer http://inchoo.net/magento/magento-custom-emails/
in above link check comments also.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment