Magento 2 how to add custom attributes to the invoice email and invoice PDF

2.5k Views Asked by At

I have a requirement that I have two attributes in each product i.e. 1. is_product_old 2. if_old_comment

At the end of invoice email and invoice pdf, I need to show if_old_comment in case is_product_old is yes for each product ordered.

My problem is I don't know which files I should Edit to customize invoice Email and Invoice pdf.

then at the end of both, I'll get the product list of that order in that invoice

And for each product, I'll show the if_old_comment in case is_product_old is yes for that product.

So I would like to know what files I need to edit for this and function that can help me to get product list for this->invoiceId and how to get the attribute of the productID.

I am in Magento 2.2.2

1

There are 1 best solutions below

0
On BEST ANSWER

Here I answer my own question others may get help from it.

to get the comment in the bottom of email template:

I created two product attributes is_product_old and if_old_comment

and extended email template for sales like this :

app/design/frontend/Codazon/fastest/ellyana/Magento_Sales/templates/email

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

// @codingStandardsIgnoreFile

?>
<?php $_order = $block->getOrder() ?>
<?php if ($_order): ?>
<?php $_items = $_order->getAllItems(); ?>
<table class="email-items">
    <thead>
        <tr>
            <th class="item-info">
                <?= /* @escapeNotVerified */  __('Items') ?>
            </th>
            <th class="item-qty">
                <?= /* @escapeNotVerified */  __('Qty') ?>
            </th>
            <th class="item-price">
                <?= /* @escapeNotVerified */  __('Price') ?>
            </th>
        </tr>
    </thead>
    <?php foreach ($_items as $_item): ?>
        <?php
            if ($_item->getParentItem()) {
                continue;
            }
        ?>
        <tbody>
            <?= $block->getItemHtml($_item) ?>
        </tbody>
    <?php endforeach; ?>
    <tfoot class="order-totals">
        <?= $block->getChildHtml('order_totals') ?>
    </tfoot>
</table>
//Here I added my code  
<table>
    <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    ?>
    <?php foreach ($_order->getAllItems() as $_item): ?>
        <?php
            if ($_item->getParentItem()) {
                continue;
            }
        ?>
        <tbody>
            <?php 
                $objectManager = Magento\Framework\App\ObjectManager::getInstance();
                $productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($_item->getSku());
                $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
                $is_refurb =   $product->getAttributeText('is_refurb');     
                if($is_refurb=='Yes'){
            ?>
                    <p><b><?= $product->getRefurbComment(); ?></b></p>
            <?php        
                }
            ?>
        </tbody>
    <?php endforeach; ?>
</table>
// here my code ends

<?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; ?>

after adding my cutsom code I got the comment If any added to each product at the bottom of the email template.