2

I am working on a new site using Magento 2.2. This is my first project using Magento 2. Can anyone guide me how to add the product SKU to the order confirmation email?

When I go to Backend -> Marketing -> Email Templates -> edit the New Order Confirmation Template, I saw this {{layout handle="sales_email_order_items" order=$order area="frontend"}} which will output the order product details. But I am not sure which file I should edit to add the product SKU, thanks.

0

1 Answer 1

5

Override vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml to your theme

app/design/frontend/vendor/theme/Magento_Sales/templates/email/items/order/default.phtml

Now add sku <td class="item-sku"><?= $block->escapeHtml($block->getSku($_item)) ?></td> where you want to show sku

I added sku after product name here is final default.phtml

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

// @codingStandardsIgnoreFile

/** @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<?php if ($block->getItemOptions()): ?> has-extra<?php endif; ?>">
        <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
        <?php if ($block->getItemOptions()): ?>
            <dl class="item-options">
            <?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)->toHtml() ?>
        <?php endif; ?>
        <?= $block->escapeHtml($_item->getDescription()) ?>
    </td>
    <!-- Here add sku column --> 
    <td class="item-sku"><?= $block->escapeHtml($block->getSku($_item)) ?></td>
    <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
    <td class="item-price">
        <?= /* @escapeNotVerified */  $block->getItemPrice($_item) ?>
    </td>
</tr>
<?php if ($_item->getGiftMessageId() && $_giftMessage = $this->helper('Magento\GiftMessage\Helper\Message')->getGiftMessage($_item->getGiftMessageId())): ?>
<tr>
    <td colspan="3" class="item-extra">
        <table class="message-gift">
            <tr>
                <td>
                    <h3><?= /* @escapeNotVerified */  __('Gift Message') ?></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>
    </td>
</tr>
<?php endif; ?>

Also you need to override vendor/magento/module-sales/view/frontend/templates/email/items.phtml for add new for sku

app/design/frontend/vendor/theme/Magento_Sales/templates/email/items.phtml

<th class="item-qty">
    <?= /* @escapeNotVerified */  __('Sku') ?>
</th>

OUTPUT: enter image description here

7
  • Do you happen to know how to add a product attribute by attribute code to the email as well?
    – Hongbin Yu
    Commented Mar 15, 2018 at 7:03
  • @PrincePatel So if I want to add my_sku attribute in email then can I use $_item->getMySku(); ? Commented Mar 20, 2018 at 5:51
  • @PrincePatel Okay. That $_tem->getId() will return item ID or product ID ? Commented Mar 20, 2018 at 6:06
  • $_item->getProduct()->getId(); will return the product ID. $_item->getMySku() will NOT return the value of the product attribute my_sku. I am actually working on add the product attribute to the email. No luck for now. I will keep you updated here once I find out.
    – Hongbin Yu
    Commented Mar 20, 2018 at 6:37
  • @KishanPatadia I have checked $_item object and it has [product_id] So you can get product id by $_item->getProductId() Commented Mar 20, 2018 at 6:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.