I have succeeded in getting the product image in the new order email template (<Vendor>/<Theme>/Magento_Sales/templates/email/items/order/default.phtml
) using the following:
<img src="<?= $this->helper('Magento\Catalog\Helper\Image')
->init($_item->getProduct(), 'product_thumbnail_image')
->constrainOnly(TRUE)
->keepAspectRatio(TRUE)
->keepTransparency(TRUE)
->keepFrame(FALSE)
->resize(200, 260)->getUrl(); ?>" alt="<?= $block->escapeHtml($_item->getName()) ?>" width="100">
But whenever I try the same code in the invoice template (<Vendor>/<Theme>/Magento_Sales/templates/email/items/invoice/default.phtml
) I receive the following error:
PHP Fatal error: Uncaught Error: Call to a member function getData() on null in /var/www/public/vendor/magento/module-catalog/Helper/Image.php:500
Stack trace:
#0 /var/www/public/vendor/magento/module-catalog/Helper/Image.php(470): Magento\\Catalog\\Helper\\Image->initBaseFile()
#1 /var/www/public/vendor/magento/module-catalog/Helper/Image.php(528): Magento\\Catalog\\Helper\\Image->applyScheduledActions()
#2 /var/www/public/app/design/frontend/Falak/englishfalak/Magento_Sales/templates/email/items/invoice/default.phtml(30): Magento\\Catalog\\Helper\\Image->getUrl()
#3 /var/www/public/vendor/magento/framework/View/TemplateEngine/Php.php(59): include('/var/www/public...')
#4 /var/www/public/vendor/magento/framework/View/Element/Template.php(270): Magento\\Framework\\View\\TemplateEngine\\Php->render(Object(Magento\\Sales\\Block\\Order\\Email\\Items\\DefaultItems\\Interceptor), '/var/www/public...', Array)
#5 /var/www/public/generated/code/Magento/Sales/Block/Order/Email/Items/DefaultItems/Interceptor.php(180): Magento\\Framework\\View\\Element\\Templa in /var/www/public/vendor/magento/module-catalog/Helper/Image.php on line 500, referer: http://lifestyley.localhost/admin/sales/invoice/view/order_id/123/invoice_id/4/key/ca0c7bf344bba27a0ed4dc1353f1f81c98c4c30764c9640ff79dd10108313c75/
Why it's not working in the invoice template but working on the order template?
The variable $_item
is there in both of them and is retrieved in the same way in both cases. And in both files, I'm using several functions on this variable successfully. The only problem seems when I try to retrieve the products pictures.