分类
服务器

Magento2 Email Template 新增变量

app/code/Maijindou/Customer/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Maijindou_Customer',
__DIR__
);
app/code/Maijindou/Customer/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Maijindou_Customer" setup_version="1.0.1">
   </module>
</config>
app/code/Maijindou/Customer/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="email_order_set_template_vars_before">
        <observer name="maijindou_customer_add_custom_variable_to_Order"
instance="Maijindou\Customer\Observer\ObserverforAddCustomVariable"/>
    </event>
</config>
app/code/Maijindou/Customer/Model/Rewrite/Order.php

<?php
namespace Maijindou\Customer\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ObserverforAddCustomVariable implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        $transport = $observer->getData('transport');
        $times = $transport->getOrder()->getCreatedAt();
        $transport['receiving_time'] = date('Y-m-d',strtotime($times)+86400);
        $observer->setData('transport',$transport);
        return $this;
    }
}

在后台模板变量中 {{var transport}}