2

I need to include product Customizable Options with price in order email template. For this I have modified the code and added the Customizable Options code. All are showing fine but the Customizable Options is not showing.

Here is my code added in

module-sales/view/frontend/templates/email/items.phtml

<?php $_order = $block->getOrder() ?>
<?php if ($_order): ?>
    <?php $_items = $_order->getAllItems(); ?>
    <table class="email-items" cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #EAEAEA;">
        <thead>
            <tr>
                <th class="item-info" align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">
                    <?= /* @escapeNotVerified */  __('Items') ?>
                </th>
                <th class="item-info" align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">
                    <?= /* @escapeNotVerified */  __('SKU') ?>
                </th>
                <th class="item-qty" align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">
                    <?= /* @escapeNotVerified */  __('Qty') ?>
                </th>
                <th class="item-price" align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px">
                    <?= /* @escapeNotVerified */  __('Price') ?>
                </th>
            </tr>
        </thead>
        <?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();           
        $option_label="";
        $option_value="";
        if (isset($options['options']))
        {        
        foreach ($options['options'] as $option)
        {
            $option_label= $option['label'];
            $option_value= $option['value'];
        }
        }
        ?>
        <tr>
        <td><?= /* @escapeNotVerified */ $option_label?></td>
        </tr>
        <tr>
        <td><?= /* @escapeNotVerified */ $option_value?></td>
        </tr>
        </table>
       </td>

            <td class="item-info" style="font-size:11px;"><?= /* @escapeNotVerified */  $_item->getSku() ?></td>
        <td align="center" class="item-info" style="font-size:11px;"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() ?></td>
            <td class="item-info" style="font-size:11px;">Rs: <?= /* @escapeNotVerified */  $_item->getPrice() ?></td>
        </tr>
                </tbody>
            <?php endif; ?>
        <?php endforeach; ?>
<tfoot class="order-totals">
  <tr class="subtotal">
        <th style="padding-right:20px;font-weight:normal;font-size:11px;" align="right" colspan="3" scope="row">
            <?= /* @escapeNotVerified */  __('Subtotal') ?> 
        </th>
        <td>
           <span style="font-size:11px;" class="price">Rs: <?= /* @escapeNotVerified */  $_order->getSubtotal() ?></span> 
        </td>
  </tr>
  <tr class="shipping">
        <th style="padding-right:20px;font-weight:normal;font-size:11px;" align="right" colspan="3" scope="row">
           <?= /* @escapeNotVerified */  __('Shipping & Handling') ?>                    
        </th>
        <td>
           <span style="font-size:11px;" class="price">Rs: <?= /* @escapeNotVerified */  $_order->getShippingAmount() ?></span> 
        </td>
   </tr>
   <tr class="grand_total">
        <th style="padding-right:20px;font-size:11px;" align="right" colspan="3" scope="row">
            <strong><?= /* @escapeNotVerified */  __('Grant total') ?></strong>
        </th>
        <td>
            <strong><span style="font-size:11px;" class="price">Rs: <?= /* @escapeNotVerified */  $_order->getGrandTotal() ?></span></strong>
        </td>
    </tr>
</tfoot>
    </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; ?>

Can anyone let me know what's the issue and how to include custom options with price in this template.

Thanks.

  • Are you check my updated my answer? – MSA Mar 25 at 10:34
  • I have added the updated code but still its showing only one Custom option value as like previous one. Please check my updated code in question – mysti Mar 25 at 10:42
0

The method getItemOptions() is not defined in the block which is used in the layout configuration for this template (Magento\Sales\Block\Order\Email\Items), therefore you can't get the options like that. You would need to call that method from the renderer block used. You should be able to get the renderer block from the Magento\Sales\Block\Order\Email\Items with something like this:

$renderer = $block->getItemRenderer($_item->getProductType())->setItem($_item);
$renderer->getItemOptions();

But anyway, I wouldnt recommend to do that. The template you have used is not meant for rendering data from each individual item. The code you have removed from the default template $block->getItemHtml($_item) renders the custom options using the renderer mentioned above. Therefore you should rather modify the template templates/email/items/order/default.phtml which is the template where the data for each order item is rendered and also the custom options are processed.

If you plan to implement more logic you may consider to create your own renderer block and extend Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder. That renderer is defined in sales_email_order_renderers.xml.

0

Following code will be helpful

<td class="item-info">
<?= /* @escapeNotVerified */  $_item->getName()?>
<table class="item-info">
<?php 
$options = $_item->getProductOptions();           
$custom_options=[];
if (isset($options['options']) )
{
foreach ($options['options'] as $option) 
{
$custom_options_label[]= $option['label'];
$custom_options_value[]= $option['value']; 
}  ?>
<?php for ($i = 0; $i < sizeof($custom_options_label); $i++)
{?>

<tr>
<td><?= /* @escapeNotVerified */ $custom_options_label[$i]?></td>
<td><?= /* @escapeNotVerified */ $custom_options_value[$i]?></td>
</tr>
<?php }

}?>
</table>
</td>

It's work like charm

  • I have added this code. Its showing only one custom option. But I have added multiple custom options. Can you please let me know how to configure this for multiple no of custom options? – mysti Mar 25 at 9:55
  • kindly check the updated answer – MSA Mar 25 at 10:14
  • I have added the updated code but still its showing only one Custom option value as like previous one. Please check my updated code in question – mysti Mar 25 at 10:36
  • magento.stackexchange.com/a/264631/60921. Hope this link will be helpful@mysti – MSA Mar 25 at 10:56
  • Yes, based on that reference code only I have added but still its showing only one custom option but I have another custom option which is missing. Why? – mysti Mar 25 at 11:01

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.