cancel
Showing results for 
Search instead for 
Did you mean: 

Display Custom product attributes on order email

Highlighted

Display Custom product attributes on order email

Hello,

 

We need to display custom attribute (Lead Time) in order confirmation email as per the attached image.

https://ibb.co/gvxQvcw

 

leadtime.png

 

Can you please advise how can I Achieve this? Our Magento version is 2.3.3

4 REPLIES 4
Highlighted

Re: Display Custom product attributes on order email

Hello @marketingi 

 

Please follow the below steps to add custom attribute after SKU in order email :

 

  1. Create a new function in Block by overriding this block class -> Magento/Sales/Block/Order/Email/Items/DefaultItems.php
    public function getCustomAttr($id){
           $product = $this->productFactory->create()->load($id);
            return $product->getCustomAttribute();
    }
  2. Override phtml of email ->
    Magento/Sales/view/frontend/templates/email/items/order/default.phtml
    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    
    // phpcs:disable Magento2.Templates.ThisInTemplate
    
    /** @var $block \Magento\Sales\Block\Order\Email\Items\DefaultItems */
    
    /** @var $_item \Magento\Sales\Model\Order\Item */
    $_item = $block->getItem();
    $_order = $_item->getOrder();
    ?>
    <tr>
        <td class="item-info<?= ($block->getItemOptions() ? ' has-extra' : '') ?>">
            <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
            <p class="sku"><?= $block->escapeHtml(__('SKU')) ?>: <?= $block->escapeHtml($block->getSku($_item)) ?></p>
            <p class="attribute"><?= $block->getCustomAttr($_item->getId()); ?></p>
            <?php if ($$block->escapeHtml(block->getItemOptions())); ?>
                <dl class="item-options">
                <?php foreach ($block->getItemOptions() as $option) : ?>
                    <dt><strong><em><?= $block->escapeHtml($option['label']) ?></em></strong></dt>
                    <dd>
                        <?= /* @noEscape */  nl2br($block->escapeHtml($option['value'])) ?>
                    </dd>
                <?php endforeach; ?>
                </dl>
            <?php endif; ?>
            <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
            <?php if ($addInfoBlock) :?>
                <?= $addInfoBlock->setItem($_item)->toHtml() ?>
            <?php endif; ?>
            <?= $block->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><?= $block->escapeHtml(__('Gift Message')) ?></h3>
                        <strong><?= $block->escapeHtml(__('From:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                        <br /><strong><?= $block->escapeHtml(__('To:')) ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                        <br /><strong><?= $block->escapeHtml(__('Message:')) ?></strong>
                        <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <?php endif; ?>

Hope it will resolve your problem !

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !
Highlighted

Re: Display Custom product attributes on order email

Hello @gaurav_harsh1 ,

 

Thank you for your reply.

 

Actually we need to display Lead Time attribute value for each item in the email (attribute code is lead_time) How to get the value of the Lead Time attribute

Highlighted

Re: Display Custom product attributes on order email

@marketingi 

 

Then your overrided class function would be :

public function getCustomAttr($id){
       $product = $this->productFactory->create()->load($id);
        return $product->getLeadTime();
}

Hope you are clear with overriding Block by creating preference and block class!

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !
Highlighted

Re: Display Custom product attributes on order email

@gaurav_harsh1 

 

Sorry I'm not clear with with overriding Block  Smiley Sad