4

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');?>">

1 Answer 1

0

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();
1
  • Hey Harry, thanks for posting. I changed this and i can pass the admin view error. But now 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 "
    – gundra
    Commented Mar 26, 2018 at 8:12

Your Answer

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

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