Magento – How to Add Transactional Email Variables in Extensions

email-templatesoverridestransactional

suppose an extension that does something to an order. Special shipping. Or maybe blue ribbon packaging.

How can this extension make available some new transactional email variables that are available in all default templates? And here comes the hard one: without overriding core classes (becuase we might not be the only one doing this). for example: {{var order.special_shipping}} or {{var shipping.special}}

Question: How can an extension make available transactional email variables in order emails without overriding core classes? (available in email templates)

Best Answer

Without rewriting core classes you cannot pass variables to the e-mail templates. This is a real disappointment for me too. I needed this numerous times.
But here is a workaround.
Instead of having custom variables you can have custom {{...}} directives.

So instead of using {{var something}} you can use directly {{something}}.
But for this you need to create your own class that handles these directives (short codes).

Create your own class Namespace_Module_Model_Template_Filter that extends Mage_Core_Email_Template_Filter where you add this method:

public function somethingDirective($construction) {
    $params = $this->_getIncludeParameters($construction[2]); 
    //do something with params
    return expected result;
} 

$params is an array with what you pass to the directive {{something param1=value1 param2=value2....}}.

Now all you need is to tell Magento to use your template filter instead of the one it uses by default.

Unfortunately I didn't find a 100% rewrite-less method.
You still have to rewrite the email template filter model. But this should not be an issue if nothing else rewrites it.

You need to add this in your config.xml

<models>
    <core> 
        <rewrite>
            <email_template_filter>Namespace_Module_Email_Template_Filter</email_template_filter>
        </rewrite>
    </core>
</models>

I know this is not exactly what you were hoping for, but it provides a cleaner way of doing it than no rewrite every method that sends an e-mail. Doing this will make your new directives available in all templates.

Even if I say it a lot, and this may look like spam, I have to quote my sources.
This idea is not mine. I got it from Vinai Kopp's book Grokking Magento

Related Topic