0

I am trying to display the alternate part number on my order confirmation.

I copied the items.phtml to example/example/Magento_Sales/templates/email/items.phtml and added the code

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

This heading displays properly on the order confirmation.

I took default.phtml and copied it to example/example/Magento_sales/templates/email/default.phtml

I added the code

 <td class="item-qty"><?= /* @escapeNotVerified */ $block->getAlternatePartNumber($_item) ?></td>

After Price so it now looks like-

<td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
<td class="item-price"><?=  /* @escapeNotVerified */ $block->getItemPrice($_item) ?></td>
<td class="item-qty"><?= /* @escapeNotVerified */ $block->getAlternatePartNumber($_item) ?></td>

It still will not display. How can I get the alternate part number to display?

Edit: Entire 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>
    <p class="sku"><?= /* @escapeNotVerified */  __('SKU') ?>: <?= 
$block->escapeHtml($block->getSku($_item)) ?></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>

<td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
<td class="item-price"><?=  /* @escapeNotVerified */ $block->getItemPrice($_item) ?></td>
<td class="item-qty"><?= /* @escapeNotVerified */ $block->getAlternatePartNumber($_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; ?>

Updated 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();
?>
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
<?php $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getId()); ?>
<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 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>

  <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
  <td class="item-price"><?=  /* @escapeNotVerified */ $block->getItemPrice($_item) ?></td>
 <td class="item-part"><?= /* @escapeNotVerified */ $product- 
>getAlternatePartNumber(); ?></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>

  • Where did you created this attribute? Also update your whole default.phtml code? – Sukumar Gorai Oct 9 '18 at 20:25
  • @SukumarGorai What do you mean where did you create this attribute? I added my entire default.phtml code. – tjjen Oct 9 '18 at 21:43
  • @tjjen, did you try script in my answer ? – Pawan Oct 15 '18 at 3:41
0

You have used $block->getAlternatePartNumber($_item), but if you print $_itemyou will not get your custom attribute here.

You need to load $_item and get custom attribute by following code:

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

<td class="item-part"><?= /* @escapeNotVerified */ $product->getAlternatePartNumber(); ?></td>

Note: Above code will show custom Attribute value, But Never use object manager directly to load model. You should use Factories

Edit I

There is no email configured on my local, So I have test above code using following script with name test.php:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$om = $bootstrap->getObjectManager();

$state = $om->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

//here your logic start

$id = 1;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);

echo $product->getAlternatePartNumber();

When I am hitting http://localhost/magento2/test.php, its giving me attribute value.

You need to debug what is not working and for that.

1. log product id
2. log attribute value
3. See all log for any error  
  • I am a little confused. It seems like you are giving me code that would work, but telling me not to use it and do it a different way. Or am I reading that wrong? – tjjen Oct 10 '18 at 13:54
  • You can use above code. – Pawan Oct 10 '18 at 14:11
  • I updated the code with your code and still nothing. Did I place it in the wrong spots? – tjjen Oct 10 '18 at 15:44
  • I did add the code in the original post so you could see. – tjjen Oct 10 '18 at 16:30
  • what is the type of your AlternatePartNumber attribute ? – Pawan Oct 10 '18 at 16:45

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.