0

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.

3 Answers 3

4

My problem has been solved (thanks to xdebug :) by replacing $_item->getProduct() that I used in the order email template with $_item->getOrderItem()->getProduct()

That's after noticing that the $_item in the invoice is different then $_item in the order and I need to use getOrderItem() function to retrieve the order item object.

1

You can use following code

$product_id=1;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id); 

Then you can get image url like this:

$this->helper('Magento\Catalog\Helper\Image')
                ->init($product, 'product_thumbnail_image')
                ->constrainOnly(TRUE)
                ->keepAspectRatio(TRUE)
                ->keepTransparency(TRUE)
                ->keepFrame(FALSE)
                ->resize(200, 260)->getUrl(); 
2
  • Thanks for the answer. But I don't want to use the ObjectManager directly. Nov 25, 2018 at 13:53
  • I have just added reference using that you can achieve your result. And also you can shift your code in block file or else you can create function for get product instance from helper class. Nov 25, 2018 at 13:56
0

You need to load all items from invoice. Most likely you'll go with loading invoice by id -> load order -> load items -> get images for each item.

Your problem can be linked to that one order can have multiple invoices.

Here is example code, I didn't test it.

<?php

$orderIncrementId = '10101010'; // See how to get order Id from invoice 
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$items = $order->getAllVisibleItems();
foreach($items as $i):
  echo $i->getProductId(); // Will return product id
  echo $i->getImageUrl(); //Not sure but will return image url
endforeach;
1
  • Thanks for your answer. But I think it's for Magento 1, not 2. Nov 25, 2018 at 11:33

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.