1

I like to get customer product image in "invoice email template"

I had add code in my below file

Magento_Sales/templates/email/items/invoice/default.phtml

<td>
        <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product_id = $_item->getProductId();
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

        $_imagehelper = $objectManager->get('Magento\Catalog\Helper\Image');
        $image_url = $_imagehelper->init($product, 'cart_page_product_thumbnail')->getUrl();

        ?>
          <img src="<?php echo $image_url; ?>" alt="<?php echo $product->getName(); ?>" />
    </td>

Image shows default for customer new invoice template.

enter image description here

1

To get an image in email template. you should use below code in order/invoice/ship item layout handle.

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product_id = $_item->getProductId();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

$_imagehelper = $objectManager->get('Magento\Catalog\Helper\Image');
$image_url = $_imagehelper->init($product, 'cart_page_product_thumbnail')->getUrl();
?>
<img src="<?php echo $image_url ; ?>" alt="<?php echo $product->getName(); ?>" />

For example to display image in Invoice email template for simple product renderer type.

Copy "sales_email_order_invoice_renderers.xml" to Vendor\YourModuleName\View\frontend\layout\sales_email_order_invoice_renderers.xml and add below code

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Creditmemo Items List" design_abstraction="custom">
    <body>
        <referenceBlock name="sales.email.order.invoice.renderers">
            <block class="Magento\Sales\Block\Order\Email\Items\DefaultItems" name="sales.email.order.invoice.renderers.default" as="default" template="Vendor_YourModuleName::email/items/invoice/default.phtml"/>
        </referenceBlock>
    </body>
</page>

Create default.phtml at Vendor\YourModuleName\templates\email\items\invoice\default.html and paste below code.

<?php $_item = $block->getItem() ?>
<?php $_order = $block->getItem()->getOrder(); ?>
<tr>
    <td>
        <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product_id = $_item->getProductId();
        $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);

        $_imagehelper = $objectManager->get('Magento\Catalog\Helper\Image');
        $image_url = $_imagehelper->init($product, 'cart_page_product_thumbnail')->getUrl();

        ?>
          <img src="<?php echo $image_url; ?>" alt="<?php echo $product->getName(); ?>" />
    </td>
    <td class="item-info<?php if ($block->getItemOptions()): ?> has-extra<?php endif; ?>">
        <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
        <p class="sku"><?= /* @escapeNotVerified */  __('SKU') ?>: <?= $block->escapeHtml($block->getSku($_item)).'test' ?></p>
        <?php if ($block->getItemOptions()): ?>
            <dl>
                <?php foreach ($block->getItemOptions() as $option): ?>
                    <dt><strong><em><?= /* @escapeNotVerified */  $option['label'] ?></em></strong></dt>
                    <dd>
                        <?= /* @escapeNotVerified */  nl2br($option['value']) ?>
                    </dd>
                <?php endforeach; ?>
            </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?= $addInfoBlock->setItem($_item->getOrderItem())->toHtml() ?>
        <?php endif; ?>
        <?= $block->escapeHtml($_item->getDescription()) ?>
    </td>
    <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQty() * 1 ?></td>
    <td class="item-price">
        <?= /* @escapeNotVerified */  $block->getItemPrice($_item->getOrderItem()) ?>
    </td>
</tr>

P.S I recommend to do not use objectmanager in phtml file directly instead override block class and use dependency.

0

Replace $_item->getProduct() that used in the order email template with $_item->getOrderItem()->getProduct()

$_item in the invoice is different then $_item in the order and need to use getOrderItem() function to retrieve the order item object.

  • Still image shows a magento placeholder image – mohana Mar 21 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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