Skip to content

Magento 2.3.5 order email observer custom block enable to pass array

I have tried the following:

Observer

...
$transport = $observer->getTransport();
$transport->setTemplateVars([
          'products' => [
            ['name' => 'Product1'],
            ['name' => 'Product2']
          ]
        ]);
...

order_new_guest.html

{{block class='Magento\Framework\View\Element\Template' area='frontend' template='Vendor_Module::email/template.phtml' products=$products}}

email/template.phtml

<p>HELLO!</p>
<?php //print_r(count($this->getProducts())); ?>
<?php foreach ($this->getProducts() as $_item): ?>
  <p><?php echo $_item['name'] ?></p>
<?php endforeach; ?>

The issue:

In foreach I get error:

Error filtering template: Warning: Invalid argument supplied for foreach() in .../htdocs/app/code/Vendor/Module/view/frontend/templates/email/template.phtml on line 3

If I count() I get error:

Error filtering template: Warning: count(): Parameter must be an array or an object that implements Countable in .../htdocs/app/code/Vendor/Module/view/frontend/templates/email/template.phtml

Am i missing something can anyone see it? Thanks in advance!