1

I want to show product custom attribute below the product image and SKU to invoice email in magento2.2.0? Any help would be appreciated.

1 Answer 1

2

You need to override vendor/magento/module-sales/view/frontend/templates/email/items/invoice/default.phtml

This is my registration.php file app/code/Amy/InvoiceEmailPcb/registration.php

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Amy_InvoiceEmailPcb" setup_version="1.0.0"/>
</config>

This is my module.xml file app/code/Amy/InvoiceEmailPcb/etc/module.xml

<?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="Amy_InvoiceEmailPcb" setup_version="1.0.0"/>
</config>

This is my sales_email_order_invoice_renderers.xml app/code/Amy/InvoiceEmailPcb/view/frontend/layout/sales_email_order_invoice_renderers.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 Creditmemo Items List" design_abstraction="custom">
    <body>
        <referenceBlock name="sales.email.order.invoice.renderers">
            <block class="Magento\Sales\Block\Order\Email\Items\DefaultItems" as="default" template="Amy_InvoiceEmailPcb::email/items/invoice/default.phtml"/>
        </referenceBlock>
    </body>
</page>

This is my default.phtml app/code/Amy/InvoiceEmailPcb/view/frontend/templates/email/items/invoice/default.phtml

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

// @codingStandardsIgnoreFile

?>
<?php $_item = $block->getItem() ?>
<?php $_order = $block->getItem()->getOrder(); ?>

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

<tr>
    <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)) ?></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; ?>

    <?php //Your code starts to show custom attribute value ?>
    <dl class="item-options">
        <dt><?= __('PCB Master') ?>:</dt>
        <dd><?php echo $product->getData('pcb_master'); ?></dd>
    </dl>
    <?php //Your code ends to show custom attribute value ?>

        <?= $block->escapeHtml($_item->getDescription()) ?>
    </td>
    <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQty() * 1 ?></td>
    <td class="item-price">
        <?= /* @escapeNotVerified */  $block->getItemPrice($_item) ?>
    </td>
</tr>
2
  • but this solution depends on saving the custom attribute also to order and invoice datamodels, not?
    – roman204
    Feb 9, 2020 at 11:38
  • I did this on theme level and it worked. Thank you.
    – Webninja
    Oct 12, 2020 at 20:48

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.