How to add custom variable

This article is about how to add custom variable to our Responsive Emails extension. To avoid any conflict with future updates, all the changes will be done in separate extension.

1. Create a new module

The new module will be called Magetrend_Custom and all the files will be located under app/code/Magetrend/Custom/ directory.

Create the following files:

app/code/Magetrend/Custom/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Magetrend_Custom',
    __DIR__
);
app/code/Magetrend/Custom/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magetrend_Custom" schema_version="2.0.0" setup_version="2.0.0">
        <sequence>
            <module name="Magetrend_Email"/>
        </sequence>
    </module>
</config>

2. Create observer and add new variable

Our extension have dedicated hooks, so all we have to do is to create an observer for event: magetrend_email_collect_additional_vars

app/code/Magetrend/Custom/etc/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="magetrend_email_collect_additional_vars">
        <observer name="mtemail_custom_var"
                  instance="Magetrend\Custom\Observer\CustomVar"/>
    </event>
</config>
app/code/Magetrend/Custom/Observer/CustomVar.php
<?php

namespace Magetrend\Custom\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomVar implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(Observer $observer)
    {
        $order = $this->getOrder($observer);
        if (!$order) {
            return;
        }

        $grandTotal = $order->formatPriceTxt($order->getGrandTotal(), false);
        $vars = $observer->getData('additional_vars');
        $vars->setData('formatted_grand_total', $grandTotal);
    }

    public function getOrder($observer)
    {
        $vars = $observer->getVars();
        if (isset($vars['order'])) {
            $order = $vars['order'];
            if ($order instanceof \Magento\Sales\Api\Data\OrderInterface) {
                return $order;
            }
        }

        if (isset($vars['invoice'])) {
            $invoice = $vars['invoice'];
            if ($invoice instanceof \Magento\Sales\Api\Data\InvoiceInterface) {
                return $invoice->getOrder();
            }
        }

        if (isset($vars['shipment'])) {
            $shipment = $vars['shipment'];
            if ($shipment instanceof \Magento\Sales\Api\Data\ShipmentInterface) {
                return $shipment->getOrder();
            }
        }

        if (isset($vars['creditmemo'])) {
            $creditMemo = $vars['creditmemo'];
            if ($creditMemo instanceof \Magento\Sales\Api\Data\CreditmemoInterface) {
                return $creditMemo->getOrder();
            }
        }

        return null;
    }
}

In this observer we added custom variable formatted_grand_total. Below are few explains of what's happening in code.

// In method getOrder there is an example how to get order object from $observer.

$order = $this->getOrder($observer);
// How to set new variable

$vars = $observer->getData('additional_vars');
$vars->setData('formatted_grand_total', $grandTotal);

In our email template we will call for this variable with construction

{{var mtVar.getFormattedGrandTotal()}}

3. Install new module and test

Connect to your ssh client and run the following commands:

php -f bin/magento setup:upgrade;
php -f bin/magento setup:di:compile;
php -f bin/magento setup:static-content:deploy -f;

That's all. Here is your new variable:

{{var mtVar.getFormattedGrandTotal()}}

You can also use this variable in if condition as following

{{if mtVar.getFormattedGrandTotal()}} YOUR CONTENT {{/if}}