3

In my magento 2.4.3 the code below was working fine to get an image in order emails. But in my test environment 2.4.5-p1 there is an error regarding the $_imageHelper

In my 2.4.3 this was in my theme Magento_Sales/templates/email/items.phtml

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
*/

// phpcs:disable Magento2.Templates.ThisInTemplate

/** @var $block \Magento\Sales\Block\Order\Email\Items */ ?> <?php $_order = $block->getOrder() ?>

<?php $_items = $_order->getAllItems(); ?>
<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #EAEAEA;">
    <thead>
        <tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">Artikel</th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">Art.nr</th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">Afbeelding</th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">Aantal</th>
            <th align="right" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">Subtotaal</th>
        </tr>        
    </thead>    
    <?php foreach ($_items as $_item): ?>
    <?php
        if ($_item->getParentItem()) {
            continue;
        }
    ?>
    <tbody>
        <?= $block->getItemHtml($_item) ?>
    </tbody>
    <?php endforeach; ?>    
    <tbody>
        <tr>
            <td colspan="5" align="right">
                <table>
                    <?= $block->getChildHtml('order_totals') ?> 
                </table>
            </td>
        </tr>
    </tbody>
</table>        
<?php if ($this->helper(\Magento\GiftMessage\Helper\Message::class)
        ->isMessagesAllowed('order', $_order, $_order->getStore())
    && $_order->getGiftMessageId()
) : ?>
    <?php $_giftMessage = $this->helper(\Magento\GiftMessage\Helper\Message::class)
        ->getGiftMessage($_order->getGiftMessageId()); ?>
    <?php if ($_giftMessage) : ?>
        <br />
        <table class="message-gift">
            <tr>
                <td>
                    <h3><?= $block->escapeHtml(__('Gift Message for this Order')) ?></h3>
                    <strong><?= $block->escapeHtml(__('From:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                    <br /><strong><?= $block->escapeHtml(__('To:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                    <br /><strong><?= $block->escapeHtml(__('Message:')) ?></strong>
                    <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                </td>
            </tr>
        </table>
    <?php endif; ?>
<?php endif; ?>

And this in Magento_Sales/templates/email/items/order/default.phtml

<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

// phpcs:disable Magento2.Templates.ThisInTemplate

/** @var $block \Magento\Sales\Block\Order\Email\Items\DefaultItems */

/** @var $_item \Magento\Sales\Model\Order\Item */ $_item = $block->getItem(); $_order = $_item->getOrder(); ?>

<!--- deze body herhalen per product --> <tbody bgcolor="#F6F6F6"> <tr> <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <strong style="font-size:11px;"><?= $block->escapeHtml($_item->getName()) ?></strong> </td> <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <?= $block->escapeHtml($block->getSku($_item)) ?> </td> <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('225','150')->getUrl();?>" alt="<?= __('Product Image');?>"> </td>

&lt;td align=&quot;center&quot; valign=&quot;top&quot; style=&quot;font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;&quot;&gt;
    &lt;?= /* @escapeNotVerified */  $_item-&gt;getQtyOrdered() * 1 ?&gt;
&lt;/td&gt;
&lt;td align=&quot;right&quot; valign=&quot;top&quot; style=&quot;font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;&quot;&gt;
    &lt;span class=&quot;price&quot;&gt;
        &lt;?= /* @noEscape */ $block-&gt;getItemPrice($_item) ?&gt;
    &lt;/span&gt;
&lt;/td&gt;

</tr>

<?php if ($_item->getGiftMessageId()
&& $_giftMessage = $this->helper(\Magento\GiftMessage\Helper\Message::class)
    ->getGiftMessage($_item->getGiftMessageId())
) : ?>
<tr>
<td colspan="3" class="item-extra">
    <table class="message-gift">
        <tr>
            <td>
                <h3><?= $block->escapeHtml(__('Gift Message')) ?></h3>
                <strong><?= $block->escapeHtml(__('From:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                <br /><strong><?= $block->escapeHtml(__('To:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                <br /><strong><?= $block->escapeHtml(__('Message:')) ?></strong>
                <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
            </td>
        </tr>
    </table>
</td>

Getting error main.CRITICAL: Exception: Warning: Undefined variable $_imageHelper

Jilco Tigchelaar
  • 411
  • 3
  • 7
  • 21

1 Answers1

4

You should declare $_imageHelper variable before use:

$_imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class);

The result, your Magento_Sales/templates/email/items/order/default.phtml file should be like the following:

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

// phpcs:disable Magento2.Templates.ThisInTemplate

/** @var $block \Magento\Sales\Block\Order\Email\Items\DefaultItems */

/** @var $_item \Magento\Sales\Model\Order\Item */ $_item = $block->getItem(); $_order = $_item->getOrder(); $_imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class); ?>

<!--- deze body herhalen per product --> <tbody bgcolor="#F6F6F6"> <tr> <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <strong style="font-size:11px;"><?= $block->escapeHtml($_item->getName()) ?></strong> </td> <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <?= $block->escapeHtml($block->getSku($_item)) ?> </td> <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"> <img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('225','150')->getUrl();?>" alt="<?= __('Product Image');?>"> </td>

    &lt;td align=&quot;center&quot; valign=&quot;top&quot; style=&quot;font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;&quot;&gt;
        &lt;?= /* @escapeNotVerified */  $_item-&gt;getQtyOrdered() * 1 ?&gt;
    &lt;/td&gt;
    &lt;td align=&quot;right&quot; valign=&quot;top&quot; style=&quot;font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;&quot;&gt;
        &lt;span class=&quot;price&quot;&gt;
            &lt;?= /* @noEscape */ $block-&gt;getItemPrice($_item) ?&gt;
        &lt;/span&gt;
    &lt;/td&gt;
&lt;/tr&gt;

</tbody> <!--- Eind body herhalen per product -->

<?php if ($_item->getGiftMessageId() && $_giftMessage = $this->helper(\Magento\GiftMessage\Helper\Message::class) ->getGiftMessage($_item->getGiftMessageId()) ) : ?> <tr> <td colspan="3" class="item-extra"> <table class="message-gift"> <tr> <td> <h3><?= $block->escapeHtml(('Gift Message')) ?></h3> <strong><?= $block->escapeHtml(('From:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?> <br /><strong><?= $block->escapeHtml(('To:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?> <br /><strong><?= $block->escapeHtml(('Message:')) ?></strong> <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?> </td> </tr> </table> </td> </tr> <?php endif; ?>

Tu Van
  • 6,868
  • 2
  • 11
  • 22