Back to Top

How to extend an email layout in a theme from a module

Updated 19 December 2022

In this blog, we are about to learn, how to extend an email layout in a theme from a module to add some content to an existing email template.

Usually, we use the provided email theme, and sometimes we would like to add some extra information or change the header/footer/content. One of the advantages of new theme layouts is that PrestaShop uses the Twig template so we can take advantage of its extension and blocks features.

To extend an email layout with HTML content in the existing email template, we need to follow the following steps:

First of all, we need to register a new hook named ‘actionListMailThemes’

We use this hook to extend HTML content in the existing email template.

Searching for an experienced
Prestashop Company ?
Find out More

Now we need to add our layout to the theme’s layout collection.
To do this we use the statement followed by the imported class namespace above the class declaration:

use PrestaShop\PrestaShop\Core\MailTemplate\Layout\Layout;
use PrestaShop\PrestaShop\Core\MailTemplate\ThemeCollectionInterface;

Now we define the definition of hook functionality as Follows:

/**
     * @param array $hookParams
     */
    public function hookActionListMailThemes(array $hookParams)
    {
        if (!isset($hookParams['mailThemes'])) {
            return;
        }

        /** @var ThemeCollectionInterface $themes */
        $themes = $hookParams['mailThemes'];
        $theme = $themes->getByName('classic');
        if (!$theme) {
            return;
        }

        // First parameter is the layout name, second one is the module name (empty value matches the core layouts)
        $orderConfLayout = $theme->getLayouts()->getLayout('order_conf', '');
        if (null === $orderConfLayout) {
            return;
        }

        //The layout collection extends from ArrayCollection so it has more feature than it seems..
        //It allows to REPLACE the existing layout easily
        $orderIndex = $theme->getLayouts()->indexOf($orderConfLayout);
        $theme->getLayouts()->offsetSet($orderIndex, new Layout(
            $orderConfLayout->getName(),
            __DIR__ . '/mails/layouts/order_conf.html.twig',
            ''
        ));
    }

We will get results as follows:

email-template-1

You can also use the constant name ThemeCatalogInterface::LIST_MAIL_THEMES_HOOK instead of actionListMailThemes.

That’s all about this blog.

If any issue or doubt in the above step, please feel free to let us know in the comment section.

We would be happy to help.

You can also learn How to add an email layout and variables in a theme from a module

You can also explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected].

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home