magento2: Transport variable can not be altered in email_invoice_set_template_vars_before Event

We waht to change the payment_html for banktransfer invoices. Unfortunately the instruction is also sent in invioce email. And there the customer already has paid the bill.

Preconditions

  1. Magento 2.1.7
  2. PHP 7.0.2
  3. MySql 5.7

Steps to reproduce

  1. Declare the event
<event name="email_invoice_set_template_vars_before">
        <observer name="mod_mail_variables_of_Invoice" instance="Vendor\Module\Observer\ChangePaymentHtml" />
    </event>
  1. Alter variable payment_html in Vendor\Module\Observer\ChangePaymentHtml like
<?php

namespace Venoor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

use Vendor\Module\Helper\Data as PaymentHelper;
use Magento\Sales\Model\Order;

class ChangePaymentHtml implements ObserverInterface
{

    protected $paymentHelper;
    public $logger;

    public function __construct(
        PaymentHelper $paymentHelper,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->paymentHelper = $paymentHelper;
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $transport = $observer->getEvent()->getTransport();
        $paymentInfo = $this->getPayment($transport['order'], $transport['store']->getStoreId());
        
        if ($paymentInfo->getTemplate() == 'info/instructions.phtml') {
            $paymentInfo->setTemplate('Vendor_Module::payment/info/invoicepaymentinfo.phtml');
        }

        $transport['payment_html'] = $paymentInfo->toHtml();
        $observer->setData('transport', $transport);
    }

    protected function getPayment(Order $order, $storeid)
    {
        return $this->paymentHelper->getRawInfoBlock(
            $order->getPayment(),
            $storeid
        );
    }
}

Expected result

  1. Email should be modified and new or changed variables should be output in email.

Actual result

  1. Email is unchanged, and $transport variable seems not to be changed.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@reviskar Here is a link to the commit in the development branch that fixes this issue.

https://github.com/magento/magento2/commit/68e7d8bbd41fe3434ae2a4b85bb0fb8c67217a0b

Unfortunately it didn’t make it in time for 2.2.2.

or 2.2.3