3

I want to add product thumbnail and custom attribute in the order email template.

Help !!!

3

\vendor\magento\module-sales\view\frontend\layout\sales_email_order_items.xml file to your custom module Company\Helloworld\view\frontend\layout\sales_email_order_items.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Order Items List" design_abstraction="custom">
    <update handle="sales_email_order_renderers"/>
    <update handle="sales_email_item_price"/>
    <body>
        <block class="Magento\Sales\Block\Order\Email\Items" name="items" template="Company_Helloworld::email/items.phtml" cacheable="false">
            <block class="Magento\Framework\View\Element\RendererList" name="sales.email.order.renderers" as="renderer.list"/>
            <block class="Magento\Sales\Block\Order\Totals" name="order_totals" template="Magento_Sales::order/totals.phtml">
                <arguments>
                    <argument name="label_properties" xsi:type="string">colspan="2"</argument>
                </arguments>
                <block class="Magento\Tax\Block\Sales\Order\Tax" name="tax" template="Magento_Tax::order/tax.phtml">
                    <action method="setIsPlaneMode">
                        <argument name="value" xsi:type="string">1</argument>
                    </action>
                </block>
            </block>
        </block>
        <block class="Magento\Framework\View\Element\Template" name="additional.product.info" template="Magento_Theme::template.phtml"/>
    </body>
</page>

Create custom module and create one custom Block as below

Company\Helloworld\Block\Example.php

<?php
namespace Company\Helloworld\Block;
use Magento\Framework\View\Element\Template;

class Example extends \Magento\Framework\View\Element\Template
{
    protected $_productModel;

    public function __construct(
        \Magento\Catalog\Model\Product $productModel
    ){
        $this->_productModel = $productModel;
    }

    public function getProduct($productId)
    {
        return  $this->_productModel->load($productId);
    }


}

then above block method we can call in Company\Helloworld\view\frontend\templates\email\items.phtml

override \vendor\magento\module-sales\view\frontend\templates\email\items.phtml to your custom module Company\Helloworld\view\frontend\templates\email\items.phtml

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

    // @codingStandardsIgnoreFile


    $blockObj = $block->getLayout()->createBlock('Company\Helloworld\Block\Example');

    ?>


    <?php $_order = $block->getOrder();
      $oriderId = $_order->getEntityId();
     ?>
    <?php if ($_order): ?>
        <?php $_items = $_order->getAllVisibleItems(); ?>
        <table>
            <thead>
                <tr>
                    <th style="width: 25%">
                        <?= /* @escapeNotVerified */  __('Items') ?>
                    </th>
                    <th>
                        <?= /* @escapeNotVerified */  __('Sku') ?>
                    </th>
                    <th style="text-align: center;">
                        <?= /* @escapeNotVerified */  __('Qty') ?>
                    </th>
                    <th style="text-align: right;">
                        <?= /* @escapeNotVerified */  __('Subtotal') ?>
                    </th>
                </tr>
            </thead>
           <?php   
           foreach ($_items as $_item):
           ?>
                <?php if (!$_item->getParentItem()) : ?>
                    <tr>
                        <?php   

                        $productId = $_item->getProductId();
                        $product = $blockObj->getProduct($productId); // your product object here
                        $productImgUrl = $this->helper('Magento\Catalog\Helper\Image')
                            ->init($product, 'product_thumbnail_image')
                            ->constrainOnly(TRUE)
                            ->keepAspectRatio(TRUE)
                            ->keepTransparency(TRUE)
                            ->keepFrame(FALSE)
                            ->resize(90, 110)->getUrl();
                        ?>
                                <td><img src= "<?php echo $productImgUrl;?>"></td>
                                <td><?php echo $product->getName();?></td>
                                <td style="text-align: center;"><?php echo intval($_item->getQtyOrdered());?></td>
                                <td style="text-align: right;"><?php echo $currency; echo number_format($_item->getRowTotal(), 2, '.', '');?></td>
                                <?php // $block->getItemHtml($_item) ?>
                    </tr>

                <?php endif; ?>
            <?php endforeach; ?> 
        </table>


         <tfoot class="order-totals">
                <?= $block->getChildHtml('order_totals') ?>
         </tfoot>



        <?php if ($this->helper('Magento\GiftMessage\Helper\Message')->isMessagesAllowed('order', $_order, $_order->getStore()) && $_order->getGiftMessageId()): ?>
            <?php $_giftMessage = $this->helper('Magento\GiftMessage\Helper\Message')->getGiftMessage($_order->getGiftMessageId()); ?>
            <?php if ($_giftMessage): ?>
                <br />
                <table class="message-gift">
                    <tr>
                        <td>
                            <h3><?= /* @escapeNotVerified */  __('Gift Message for this Order') ?></h3>
                            <strong><?= /* @escapeNotVerified */  __('From:') ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                            <br /><strong><?= /* @escapeNotVerified */  __('To:') ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                            <br /><strong><?= /* @escapeNotVerified */  __('Message:') ?></strong>
                            <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                        </td>
                    </tr>
                </table>
            <?php endif; ?>
        <?php endif; ?>
    <?php endif; ?>
| improve this answer | |
  • I will update you. Thanks! – Masud Shaikh Jan 6 at 8:30
  • This is a terrible way of achieving this, the answer from Adam is substantially better – eddhall Jan 6 at 17:21
4

The accepted answer is a very hacky way to achieve what you want and I wouldn't recommend it.

If you override the file vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml you can add the thumbnail easily enough, since you have access to the full product model, as well as your custom attributes:

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

// ...
/** @var $product \Magento\Catalog\Model\Product|null */
$product = $_item->getProduct();
?>

Thumbnail URL path: <?= /** @escapeNotVerified */ $product->getData('thumbnail') ?? 'no thumbnail'; ?>
My Custom Attribute: <?= /** @escapeNotVerified */ $product->getData('custom_attribute_name'); ?>

Since the underlying code of \Magento\Sales\Model\Order\Item::getProduct() calls \Magento\Catalog\Model\Product::load($id) you should be able to grab your custom attributes this way.

If you need to resize the product image, the helper method is an appropriate way of doing it like Hitesh suggests:

// same code as before
$fullImageUrl = $this->helper('\Magento\Catalog\Helper\Image')->init($product, 'my_product_image_thumbnail_id')->/* other params */->resize(100,100)->getUrl();
| improve this answer | |

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.