1

I have tried the following:

Observer

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

order_new_guest.html

{{layout handle="sales_email_order_vendor_module_items" order=$order area="frontend"}}' 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

UPDATED:

<?xml version="1.0"?>
<page  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <block class="Magento\Framework\View\Element\Template" name="sales_email_order_vendor_module_items" template="Vendor_Module::email/template.phtml"/>
  </body>
</page>

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

2
  • please add your layout xml file also as well. Oct 4, 2022 at 9:21
  • @RohanHapani Thanks, I've update with layout xml now Oct 4, 2022 at 10:10

2 Answers 2

0

I noticed you've updated the html email template to load the phtml file using layout (good point as this is how magento core did).

Do you still have the PHP error?

It's worth change $this->getProducts() to $block->getProducts(). In phtml files, magento 2 uses $block instead of $this.

Your code which setTemplateVars will remove all template variables:

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

It should be changed as the following:

$transport->setData('products',
 ['name' => 'Product1'],
 ['qty' => '1']
);
$transport->setTemplateVars($transport->getData());
1
  • trying you solution still same errors. however I have other text fields which I can pick by doing {{var myVar}} I found a solution that works! will post in the answer soon Oct 4, 2022 at 12:46
0

Found a solution that works! First of using the xml updates but I've created a block class and use that block class Email instead of Magento\Framework\View\Element\Template like so:

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class Email extends Template
{
  
  /**
   * Constructor
   *
   * @param Context $context
   * @param array   $data
   */
  public function __construct(
    Context $context,
    array $data = []
  ) {
    parent::__construct($context, $data);
  }

}

Xml Layout

<?xml version="1.0"?>
<page  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <block class="Vendor\Module\Block\Email" name="sales_email_order_vendor_module_items" template="Vendor_Module::email/template.phtml"/>
  </body>
</page>

Secondly none of the setTemplateVars setData etc... worked, I added the bellow in my Observer:

...
$transport['products'] = [
      ['name' => 'Product1'],
      ['name' => 'Product2']
    ];
...

order_new_guest.html and email/template.phtml kept the same: and I get the email here is a screen shot: enter image description here

This for me in my case is complete and one piece of the puzzle I haven't found searching the internet on examples where you have your own block that implements Magento\Framework\View\Element\Template otherwise it wouldn't work even though the template.ptml was loading correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.