Skip to content

Transport variable can not be altered in email_invoice_set_template_vars_before Event #10210

Closed
@diybook

Description

@diybook

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.

Metadata

Metadata

Assignees

Labels

Component: PaymentFixed in 2.1.xThe issue has been fixed in 2.1 release lineFixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releasebug report

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions