1

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)

2 Answers 2

7

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

4
  • no worries. Didn't know that one. Bought the book.
    – snh_nl
    Nov 28, 2014 at 14:07
  • Wouldn't adding a dispatchEvent like @Cristi mentions be the easiest solution to adding a simple method to do this? (if yes, then I think this is worth logging)
    – snh_nl
    Nov 28, 2014 at 14:09
  • 1
    If there would be an event dispatched for the e-mail it would be nice. But I didn't find a useable one so far. And I needed what you need a lot of times.
    – Marius
    Nov 28, 2014 at 14:12
  • And maybe something like this can be a solution maybe when you only need 1 sentence or block of text: magento.stackexchange.com/questions/7337/…
    – snh_nl
    Nov 28, 2014 at 15:06
0

all variables are sent (replaced) to the transactional email template when the actual email is being sent. so you need to change that part, the one that sends the email, either through a rewrite or an observer. you'll also need to add the variables in the template, ideally using database transactional templates, and not changing the core ones

5
  • Hi thx. Could you maybe elaborate a little on "all variables are sent (replaced) to the transactional email template when the actual email is being sent. so you need to change that part, the one that sends the email, either through a rewrite or an observer."
    – snh_nl
    Nov 28, 2014 at 13:38
  • I know we need to add them in the template. But the question is more about: how to add new variables through code. that are available in order and shipping emails (pref without overrides)
    – snh_nl
    Nov 28, 2014 at 13:39
  • if you look in Mage/Sales/Model/Order.php, around line 1330 (on version 1.9.0), in the qsendNewOrderEmailq method, you'll notice how the email is constructed: $mailer->setTemplateParams(array(...)). $mailer is Mage::getModel('core/email_template_mailer') so you could rewrite this, but that would be way more complicated. So, from the top of my head, i would create a rewrite of Mage_Sales_Model_Order and overload sendNewOrderEmail
    – Cristi
    Nov 28, 2014 at 13:57
  • I'll have a look. thanks. But seems that the only way it to overwrite? There is no event or observer method that we could use - not overriding any core files!
    – snh_nl
    Nov 28, 2014 at 14:01
  • i didn't see any dispatchEvent calls, so I'd say "no events". it would be nice, totally agree. take a look, maybe i missed some lines
    – Cristi
    Nov 28, 2014 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.