cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Attribute into Order Confirmation Email

Custom Attribute into Order Confirmation Email

Hello,

        I am trying to fetch the custom attribute ( Type text ) value into the order confirmation email. For that, I am using the following code

 

 

<?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; ?>
        <p class="msg"><?= /* @escapeNotVerified */  __('Custom Comment') ?>: <?= $custom_comment = $block->getAttributeText('custom_comment') ?></p>
        <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>

I have added new line 

 

<p class="msg"><?= /* @escapeNotVerified */ __('Custom Comment') ?>: <?= $custom_comment = $block->getAttributeText('custom_comment') ?></p>

 

to get Custom comment msg added to each product in the backend, and once the order is placed no value is returned for that field. 

 

Is there something else I have to do to make this value fetched for each item in order confirmation email under SKU ?

 

3 REPLIES 3

Re: Custom Attribute into Order Confirmation Email

I have checked above code is working with dropdown type of custom attribute, only the text type is not working.

Re: Custom Attribute into Order Confirmation Email

Hello @mr_bukhari 

 

Please follow the below steps to add custom attribute 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 helps !

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: Custom Attribute into Order Confirmation Email

I've followed the above mentioned procedure it's working for the simple product,

But for the grouped product I didn't get the attribute in order confirmation email.

 

I've ordered a simple product associated to the grouped product, the product attribute was assigned all the simple product (Associated simple product of the grouped product)

In order confirmation email, the custom product attribute is not binded.

 

Your support is most appriciated one