0

I have a custom input field on each product item in the cart. The value is added to the sales-order-item table. The field is exposed to the order api as a custom attribute. I want to add the value of this field to each order item listed in the new order email that is sent to the customer. Can anyone suggest how to do this?

1 Answer 1

1

You need to override the items.phtml code

Path:app/design/frontend/Theme/Magento_Sales/templates/email/items.phtml

And you can add your custom attribute like below,

            <td class="item-qty">
                <?= /* @escapeNotVerified */  $_item->getSku() ?>
            </td>
            <td class="item-price">
                <?= /* @escapeNotVerified */  $_item->getQtyOrdered() ?>
            </td>
            <td class="item-sku">
                <?= /* @escapeNotVerified */  $_item->getPrice() ?>
            </td>
           <td class="item-custom-attribute">
                <?= /* @escapeNotVerified */  $_item->getCustomAtrribute() ?>
            </td>

here is overrided file,

<?php $_order = $block->getOrder() ?>
<?php if ($_order): ?>
<?php $_items = $_order->getAllItems();
 ?>



<table class="email-items" style="width: 650px; text-align="left"">

        <tr>
            <td style="background-color:#EAEAEA; width: 320px;  ">
                <?= /* @escapeNotVerified */  __('Items') ?>
            </td>
            <td style="background-color:#EAEAEA; width: 100px; ">
                <?= /* @escapeNotVerified */  __('Sku') ?>
            </td>
            <td style="background-color:#EAEAEA;width: 100px; ">
                <?= /* @escapeNotVerified */  __('Qty') ?>
            </td>
            <td style="background-color:#EAEAEA; width: 120px;">
                <?= /* @escapeNotVerified */  __('Subtotal') ?>
            </td>
        </tr>

    <?php foreach ($_items as $_item): ?>
        <?php if (!$_item->getParentItem()) : ?>
            <tbody>
              <tr>
            <td class="item-info">
                <?= /* @escapeNotVerified */  $_item->getName()?>
               <table class="item-info">

        <?php 
            $options = $_item->getProductOptions();           
            $s="";
            $d="";
                if (isset($options['options']) ) {        
                    foreach ($options['options'] as $option) {
                        $s= $option['label'];
                        $d= $option['value'];
                    }
                }
        ?>

                   <tr class="item-price">
                       <td class="item-price"><?= /* @escapeNotVerified */ $s?></td>
                   </tr>
                   <tr class="item-price">
                       <td class="item-price"><?= /* @escapeNotVerified */ $d?></td>
                   </tr>
               </table>
            </td>
            <td class="item-qty">
                <?= /* @escapeNotVerified */  $_item->getSku() ?>
            </td>
            <td class="item-price">
                <?= /* @escapeNotVerified */  $_item->getQtyOrdered() ?>
            </td>
            <td class="item-sku">
                <?= /* @escapeNotVerified */  $_item->getPrice() ?>
            </td>
        </tr>
            </tbody>
        <?php endif; ?>
    <?php endforeach; ?></table>
       <table align="right" style="border-collapse: separate;
            border-spacing: 0 1em; width: 650px;">
        <tr>
            <td align="right"><?= /* @escapeNotVerified */  __('Subtotal') ?></td>
            <td align="right">  <?= /* @escapeNotVerified */  $_order->getSubtotal() ?></td>
        </tr>
        <tr>
            <td align="right"><?= /* @escapeNotVerified */  __('Shipping & Handling') ?></td>
            <td align="right"><?= /* @escapeNotVerified */  $_order->getShippingAmount() ?></td>
        </tr>
        <tr>
            <td align="right"><?= /* @escapeNotVerified */  __('Tax') ?></td>
            <td align="right"><?= /* @escapeNotVerified */  $_order->getTaxAmount() ?></td>
        </tr>
        <tr>
            <th align="right"><?= /* @escapeNotVerified */  __('Grant total') ?></th>
            <th align="right"><?= /* @escapeNotVerified */  $_order->getGrandTotal() ?></th>
        </tr>
    </table>

    <?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; ?>
<?php endif; ?>
2
  • Thank you - works well for me
    – nickcobb
    Dec 28, 2023 at 14:25
  • Please accept it and upvote it, thanks:) Dec 28, 2023 at 14:30

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.