Get Free Audit

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

Mar 7, 2017

4043 Alex Simonchik

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

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.

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


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.

Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

9 Comments

  1. 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.

  2. 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

  3. 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.

  4. 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

  5. Hi, Stefano,

    Indicate here shipping instead of order_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!

  6. 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

  7. 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.

  8. 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?

  9. 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?

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *