Magento 2 email template config directives

It can be quite comfortable to use configuration values in Magento 2 email templates. Let us assume we change our store phone number in the configurations and it changes also in templates where we use sentences like: You can call us any time at .... I will show pretty fast in this post how to use the restricted list of configs in email templates and will explain ways to add your own configuration paths.

Include config values in email temaplets

We can include config values in email temaplets with two opening curly brackets {{ followed by the string config, whitespace, the path parameter and two closing curly brackets }}. Here a small exmaple that includes the config value of general/store_information/phone embedded in a span tag.

<span>{{config path="general/store_information/phone"}}</span>

Restricted list of configs

Unfortunately is there just a small restricted list of configuration values that can be used at the moment. Here are the available values.

pathlabel
web/unsecure/base_urlBase Unsecure URL
web/secure/base_urlBase Secure URL
trans_email/ident_general/nameGeneral Contact Name
trans_email/ident_general/emailGeneral Contact Email
trans_email/ident_sales/nameSales Representative Contact Name
trans_email/ident_sales/emailSales Representative Contact Email
trans_email/ident_custom1/nameCustom1 Contact Name
trans_email/ident_custom1/emailCustom1 Contact Email
trans_email/ident_custom2/nameCustom2 Contact Name
trans_email/ident_custom2/emailCustom2 Contact Email
general/store_information/nameStore Name
general/store_information/phoneStore Phone Number
general/store_information/hoursStore Hours
general/store_information/country_idCountry
general/store_information/region_idRegion/State
general/store_information/postcodeZip/Postal Code
general/store_information/cityCity
general/store_information/street_line1Street Address 1
general/store_information/street_line2Street Address 2

You can try to add your own config path, but it won't work.

Expand the list usable configs

Magento will only allow the values from above that can be found in class Magento\Email\Model\Source\Variables. Magento 2 is doing so much stuff with configurations that does not make much sense to me, but those things are just in a protected class varaibale.

Preference Variables

A possible way to add your own values is use preference and to replace the Variables class. For example like this in a di.xml file.

<config>
    <preference for="Magento\Email\Model\Source\Variables" type="Lesti\Example\Model\Source\Variables" />
</config>

Now we just have to implement the prefered class that can look something like this.

<?php
namespace Lesti\Example\Model\Source;

class Variables extends \Magento\Email\Model\Source\Variables
{
    public function __construct()
    {
        parent::__construct();
        // here I'm adding my own values
        $this->_configVariables[] = ['value' => 'general/imprint/ceo', 'label' => __('CEO Bitch')];
    }
}

Plugins

Instead of the solution from above we can use plugins. Here is the way it is done in firegento/firegento-magesetup2. This is pretty cool, cause firegento/firegento-magesetup2 provides a lot of nice configurations for the imprint that can be reused in the email temaples.

Here a part of etc/di.xml

<?xml version="1.0"?>
    <!-- ... -->
    <type name="Magento\Email\Model\Source\Variables">
        <plugin name="mageSetupEmailSourceVariables" type="FireGento\MageSetup\Plugin\Email\Model\Source\Variables"/>
    </type>

    <type name="Magento\Email\Block\Adminhtml\Template\Edit\Form">
        <plugin name="mageSetupEmailAdminhtmlTemplateEditForm" type="FireGento\MageSetup\Plugin\Email\Block\Adminhtml\Template\Edit\Form"/>
    </type>
    <!-- ... -->
</config>

Variables

The class FireGento\MageSetup\Plugin\Email\Model\Source\Variables provides the new config variables and appends them with the afterGetData function after the getData function of Magento\Email\Model\Source\Variables.

Form

The class FireGento\MageSetup\Plugin\Email\Block\Adminhtml\Template\Edit\Form appends the new config values with the afterGetVariables function after the getVariables function of Magento\Email\Block\Adminhtml\Template\Edit\Form. That way the admin user can see the variables in a special section if he clicks the button Insert Variable....

Next Previous