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.
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>
Unfortunately is there just a small restricted list of configuration values that can be used at the moment. Here are the available values.
path | label |
---|---|
web/unsecure/base_url | Base Unsecure URL |
web/secure/base_url | Base Secure URL |
trans_email/ident_general/name | General Contact Name |
trans_email/ident_general/email | General Contact Email |
trans_email/ident_sales/name | Sales Representative Contact Name |
trans_email/ident_sales/email | Sales Representative Contact Email |
trans_email/ident_custom1/name | Custom1 Contact Name |
trans_email/ident_custom1/email | Custom1 Contact Email |
trans_email/ident_custom2/name | Custom2 Contact Name |
trans_email/ident_custom2/email | Custom2 Contact Email |
general/store_information/name | Store Name |
general/store_information/phone | Store Phone Number |
general/store_information/hours | Store Hours |
general/store_information/country_id | Country |
general/store_information/region_id | Region/State |
general/store_information/postcode | Zip/Postal Code |
general/store_information/city | City |
general/store_information/street_line1 | Street Address 1 |
general/store_information/street_line2 | Street Address 2 |
You can try to add your own config path, but it won't work.
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.
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')];
}
}
preference
at
Abstraction-Implementation mappingsInstead 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>
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
.
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....
after
plugins at
After methods