Magento – (Solved) Magento 2, Add product image in all transaction emails(shipment, invoice, credit memo)

magento2product-imagestransactional-mail

I have managed to add products images in new order email using this solution:
Magento 2 product image in new order email

Unfortunately this piece of code is not working for other transactional emails:

  • credit memo
  • new invoice
  • new shipment

New Shipment email that i have received is showing error:

" Error filtering template: Notice: Undefined variable: _order in
/var/www/html/app/design/frontend/Packers/luma/Magento_Sales/templates/email/items/shipment/default.phtml
on line 12 "

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

// @codingStandardsIgnoreFile

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

$_order = $_item->getOrder();   //   -->  line 12
$_store = $_order->getStore();

/* product images */
$_imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Helper\Image');
$_baseImageUrl = $_store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product';



?>
<tr>
    <td>
         <img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('150','150')->getUrl();?>" alt="<?= __('Product Image');?>">
    </td>

Is there any solution for getting product images displayed on all transactional emails?


WORKING SOLUTION (for all transactional emails):

<?php

$_item = $block->getItem() 

$productId = $_item->getProductId();
$objectManagerHere = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManagerHere->get('Magento\Catalog\Model\Product')->load($productId);
$productImage = $this->helper('Magento\Catalog\Helper\Image')->init($product, 'category_page_list', array('height' => '100' , 'width'=> '100'))->getUrl();
?>

<img height="100" width="100" src="<?= $productImage ?>" alt="<?= __('Product Image');?>">

Best Answer

You're missing a semicolon in the first line. Try this...

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