Today we’ll see how to add new Order email item image in Magento 2.
First You Need to Create Custom module then follow this steps. I have shared a step by step guide to add cms block on checkout sidebar.
Step 1: Create register.php
app/code/Bluethinkinc/NewOrderEmailItem/registration.php
1 2 3 4 5 |
<?php use Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Bluethinkinc_NewOrderEmailItem', __DIR__); ?> |
Step 2: Create module.xml
app/code/Bluethinkinc/NewOrderEmailItem/etc/module.xml
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethinkinc_NewOrderEmailItem" setup_version="1.1.0"> </module> </config> |
Step 3: Create sales_email_order_renderers.xml
app/code/Bluethinkinc/NewOrderEmailItem/view/frontend/layout/sales_email_order_renderers.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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 Creditmemo Items List" design_abstraction="custom"> <body> <referenceBlock name="sales.email.order.renderers"> <block class="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" name="sales.email.order.renderers.default" as="default" template="Bluethinkinc_NewOrderEmailItem::email/items/order/default.phtml"/> </referenceBlock> </body> </page> |
Step 4: Create default.phtml
app/code/Bluethinkinc/NewOrderEmailItem/view/frontend/templates/email/items/order/default.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ // phpcs:disable Magento2.Templates.ThisInTemplate // phpcs:disable Magento2.Files.LineLength, Generic.Files.LineLength /** @var $block \Magento\Sales\Block\Order\Email\Items\DefaultItems */ /** @var $_item \Magento\Sales\Model\Order\Item */ /** @var \Magento\Framework\Escaper $escaper */ $_item = $block->getItem(); $_order = $_item->getOrder(); $_store = $_order->getStore(); $product = $_item->getProduct(); $_baseImageUrl = $_store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'catalog/product'; if($childProd = current($_item->getChildrenItems())) { $productImage = $_baseImageUrl . $childProd->getProduct()->getImage(); } else { $productImage = $_baseImageUrl . $product->getImage(); } ?> <tr> <td class="item-info<?= ($block->getItemOptions() ? ' has-extra' : '') ?>"> <img src="<?= $productImage?>" alt="<?= __('Product Image');?>" width="70" height="70"> <p class="product-name"><?= $escaper->escapeHtml($_item->getName()) ?></p> <p class="sku"><?= $escaper->escapeHtml(__('SKU')) ?>: <?= $escaper->escapeHtml($block->getSku($_item)) ?></p> <?php if ($block->getItemOptions()): ?> <dl class="item-options"> <?php foreach ($block->getItemOptions() as $option): ?> <dt><strong><em><?= $escaper->escapeHtml($option['label']) ?></em></strong></dt> <dd> <?= /* @noEscape */ nl2br($option['value']) ?> </dd> <?php endforeach; ?> </dl> <?php endif; ?> <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?> <?php if ($addInfoBlock):?> <?= $addInfoBlock->setItem($_item)->toHtml() ?> <?php endif; ?> <?= $escaper->escapeHtml($_item->getDescription()) ?> </td> <td class="item-qty"><?= (float) $_item->getQtyOrdered() ?></td> <td class="item-price"> <?= /* @noEscape */ $block->getItemPrice($_item) ?> </td> </tr> <?php if ($_item->getGiftMessageId() && $_giftMessage = $this->helper(\Magento\GiftMessage\Helper\Message::class) ->getGiftMessage($_item->getGiftMessageId()) ): ?> <tr> <td colspan="3" class="item-extra"> <table class="message-gift"> <tr> <td> <h3><?= $escaper->escapeHtml(__('Gift Message')) ?></h3> <strong><?= $escaper->escapeHtml(__('From:')) ?></strong> <?= $escaper->escapeHtml($_giftMessage->getSender()) ?> <br /><strong><?= $escaper->escapeHtml(__('To:')) ?></strong> <?= $escaper->escapeHtml($_giftMessage->getRecipient()) ?> <br /><strong><?= $escaper->escapeHtml(__('Message:')) ?></strong> <br /><?= $escaper->escapeHtml($_giftMessage->getMessage()) ?> </td> </tr> </table> </td> </tr> <?php endif; ?> |
Step 5: After create the module if you run the command as
php bin/magento module:status
You should see the module is disable now:
List of disabled modules: Bluethinkinc_ NewOrderEmailItem
enable the module right now, let run the command as:
php bin/magento module:enable Bluethinkinc_ NewOrderEmailItem
After Enable Module Then Run this Command
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento c:f
sudo chmod -R 777 var/ pub/static generated/
Now, please pleace order and check your email and see the result
Mukesh Singh
2023-11-21