How To Manage Module’s Variables in Emails in Prestashop 1.6 – 1.7 + ThirtyBees

March 7, 2017 / Posted in Prestashop, Prestashop 1.7 by Alex Simonchik
How To Manage Module's Variables in Emails in Prestashop 1.6 - 1.7 + ThirtyBees
Download PDF

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.

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:

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.

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.



Post a new comment