When developing new modules we may have the necessity to add some module’s data into e-mails. Let’s consider several approaches how it is possible to implement that:
1. Using the hook actionEmailAddAfterContent lets you modify content before sending an e-mail. This method is very user-friendly because a user does not need to edit e-mail template himself since the hook can modify html on the fly.
2. The second approach lets you manage separate variables on your own. This method gives users the possibility to put variables on any place in the e-mail text.
The first approach:
Register your module in the hook actionEmailAddAfterContent and implement the method: hookActionEmailAddAfterContent
The hook is called in the Send() method of the Mail – Mail::Send() class.
1 2 3 4 5 6 7 8 9 10 11 12 |
public function hookActionEmailAddAfterContent($params) { $content = ''; if ($params['template'] == 'order_conf') { // Let's edit content of Order's Confirmation email $content .= '<tr class="conf_body" id="custom_container"> <td bgcolor="#f8f8f8" colspan="8" style="border:1px solid #D6D4D4;color:#333;padding:7px 0"> ...Hello World </td> </tr>'; // where add custom text } $params['template_html'] = str_replace("{products}", "{products}" . $content, $params['template_html']); // and add text to end of {products} variable } |
The best thing is that $params gets transmitted via a link, and this lets us modify content in our method.
The second approach is based on the function OrderHistory::sendEmail(), specifically, on this part:
1 2 3 4 5 6 7 8 |
... if ($result['module_name']) { $module = Module::getInstanceByName($result['module_name']); if (Validate::isLoadedObject($module) && isset($module->extra_mail_vars) && is_array($module->extra_mail_vars)) { $data = array_merge($data, $module->extra_mail_vars); } } ... |
Using this implementation, variables will become available only when sending e-mails that are related to the status of the order which has been generated by the module. For that we should:
1. In our module define the variable $extra_mail_vars, which will have all available e-mail variables described.
2. Register the module in the hook actionValidateOrder to have the possibility to add new values to variables when sending e-mails about new orders.
3. Initialize variables in the constructor to be able to access them when sending e-mails manually from the Backoffice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public $extra_mail_vars = array( '{var1}' => '', '{var2}' => '', '{varN}' => '' ); // available email's variables public function __construct() { .... if (Tools::isSubmit('id_order')) { //case when Employee send email manually $awesome_data = BelvgAwesomeModule::getDataByOrderId(Tools::getValue('id_order')); // function for get values of variables bases on Order ID $this->extra_mail_vars['{var1}'] = $awesome_data['var1']; // apply value to variable $this->extra_mail_vars['{var2}'] = $awesome_data['var2']; $this->extra_mail_vars['{varN}'] = $awesome_data['varN']; } } public function hookActionValidateOrder($params) { //for cases when email sends automatically for new orders if ($params['order']->module == 'belvg_awesomemodule') { $awesome_data = BelvgAwesomeModule::getDataByOrderId($params['order']->id); $this->extra_mail_vars['{var1}'] = $awesome_data['var1']; $this->extra_mail_vars['{var2}'] = $awesome_data['var2']; $this->extra_mail_vars['{varN}'] = $awesome_data['varN']; } } .... |
We should also note that since Prestashop 1.7 the Mail class has been given many new hooks, but the above approaches are also good for Prestashop 1.6 – 1.7, and also for the ThirtyBees fork project.
Hi Stefano,
it’s pretty complicated to provide the right answer to your question without getting into details.
The best way to solve your issue is to contact our support team [email protected]. Our specialists will be glad to help for an additional charge.
Hi Alex,
I have to write in other email, not order_conf email, all the variables. So I don’t know how to do it and I ask some help and how start this work.
thanks
Stefano,
if you already have custom template this means that some override/module handle the send action, you can define these variables at this place.
By the way, creating a module is the best way to add new features to the Prestashop, but you can add override if you can’t reach desired behavior with a module.
Hi Alex,
maybe I have more difficult. I want to add some custom variables, product, prodcut link, product sku, customer email, customer id in my custom template, shipped order.
So the right thing to do this is create a module?
So if I have to create a module, which is the right way?
thanks
Hi, Stefano,
Indicate here
shipping
instead oforder_conf
if ($params['template'] == 'order_conf') { // Let's edit content of Order's Confirmation email
(It’s line #3 in the article).
Hope, it will help!
Hi,
I want to add some variables in my custom email template, in my case “shipping” template.
So can I do this with the hook?
thanks
Dear Azz,
the issue is pretty complicated. Unfortunately, it’s not possible to provide a qualitative answer without getting into details.
Please contact our support department in case you need an individual consideration of the matter: [email protected]
We’ll be happy to assist.
from that what see extra_mail_vars only needs to be declared in __construct() which i didn’t done.
Could you help me get order data in constructor?
hello,
i’ve tried your suggestion and nothing happening. Variable is not changing in emails
public function hookActionValidateOrder($params)
{
if ($params[‘order’]->module == ‘my_module’) {
$this->extra_mail_vars[‘{total_paid_tax_excluded}’] = $params[‘order’]->total_paid_tax_excl;
}
}
i also tried without if statement, but same result. variable ‘total_paid_tax_excluded’ is not changing in emails. Could you help?