Magento – How to add custom information to an order item in Order Sale Email in Magento 2.2.4

magento2.2.4

The default email Order Confirmation Template includes the Order Items Grid variable.

I need to add custom information to the first column.
By default, the first column in the grid looks contains just the item name and item SKU.
I need to call my own function for each item and generate a code that needs to be added right after item SKU.

Where do I do it programmatically?

Best Answer

Here is the solution I have customized sales_order_invoice area so you can customized any area which you want only you have to follow the steps only your are will be different.

files are below in which you need to customize:-

Namespace/Modulename/view/frontend/layout/sales_order_invoice.xml
Namespace/Modulename/etc/frontend/di.xml
Namespace/Modulename/Block/Order/Invoice/Items.php
Namespace/Modulename/view/frontend/templates/order/invoice/items.phtml

sales_order_invoice.xml

<?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">
    <body>
       <referenceBlock name="invoice_items">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Namespace_ModuleName::order/invoice/items.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\Invoice\Items" type="Namespace\ModuleName\Block\Order\Invoice\Items" />
</config>

Items.php :- You block file you will create function here to access in your phtml file.

<?php

namespace Namespace\ModuleName\Block\Order\Invoice;

class Items extends \Magento\Sales\Block\Order\Invoice\Items
{

     */
    private $orderItem;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Sales\Model\Order\Item $orderItem,
        array $data = []
    ) {
        parent::__construct($context, $registry, $data);
        $this->orderItem = $orderItem;
    }

    public function YourCustomFunction()
    {
      //You code will be here
    }


}

Items.phtml :- here is the file where you will add your custom information

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

// @codingStandardsIgnoreFile

?>
<?php $_order = $block->getOrder() ?>
<div class="actions-toolbar">
    <a href="<?= /* @escapeNotVerified */ $block->getPrintAllInvoicesUrl($_order) ?>"
       target="_blank"
       class="action print">
        <span><?= /* @escapeNotVerified */ __('Print All Invoices') ?></span>
    </a>
</div>
<?php foreach ($_order->getInvoiceCollection() as $_invoice): ?>
<div class="order-title">
    <strong><?= /* @escapeNotVerified */ __('Invoice #') ?><?= /* @escapeNotVerified */ $_invoice->getIncrementId() ?></strong>
    <a href="<?= /* @escapeNotVerified */ $block->getPrintInvoiceUrl($_invoice) ?>"
       onclick="this.target='_blank'"
       class="action print">
        <span><?= /* @escapeNotVerified */ __('Print Invoice') ?></span>
    </a>
</div>
<div class="table-wrapper table-order-items invoice">
    <table class="data table table-order-items invoice" id="my-invoice-table-<?= /* @escapeNotVerified */ $_invoice->getId() ?>">
        <caption class="table-caption"><?= /* @escapeNotVerified */ __('Items Invoiced') ?></caption>
        <thead>
            <tr>
                <th class="col name"><?= /* @escapeNotVerified */ __('Product Name') ?></th>
                <th class="col sku"><?= /* @escapeNotVerified */ __('SKU') ?></th>
                <th class="col price"><?= /* @escapeNotVerified */ __('Price') ?></th>
                <th class="col qty"><?= /* @escapeNotVerified */ __('Qty Invoiced') ?></th>
                <th class="col subtotal"><?= /* @escapeNotVerified */ __('Subtotal') ?></th>
            </tr>
        </thead>
        <?php $_items = $_invoice->getAllItems(); ?>
        <?php $_count = count($_items) ?>
        <?php foreach ($_items as $_item): ?>
        <?php if ($_item->getOrderItem()->getParentItem()) {
    continue;
} ?>
        <tbody>
            <?= $block->getItemHtml($_item) ?>
        </tbody>
        <?php endforeach; ?>
        <tfoot>
            <?= $block->getInvoiceTotalsHtml($_invoice) ?>
        </tfoot>
    </table>
</div>
<?= $block->getInvoiceCommentsHtml($_invoice) ?>
<?php endforeach; ?>

I hope this will help you.

Related Topic