0

how do I get bundle product options labels with value in my custom email templates? My custom email template code is here..

<?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 */
?>
<?php $_order = $block->getOrder() ?>
<?php $_imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class); ?>
<?php if ($_order) : ?>
    <?php $_items = $_order->getAllVisibleItems(); ?>
    <table class="email-items">
        <thead>
            <tr>
                <th>
                </th>
                <th class="item-info">
                    <?= $block->escapeHtml(__('Product')) ?>
                </th>
                <th class="item-info">
                    <?= $block->escapeHtml(__('SKU')) ?>
                </th>
                <th class="item-info">
                    <?= $block->escapeHtml(__('Qty')) ?>
                </th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($_items as $_item) : ?>
            <tr>
                <td>
                    <?php if($_item->getProduct()): ?>
                    <img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('120','120')->getUrl();?>" alt="<?= __('Product Image');?>">
                    <?php endif; ?>
                </td>
                <td>
                    <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
                </td>
                <td>
                    <p class="product-sku"><?= $block->escapeHtml($_item->getSku()) ?></p>
                </td>
                <td class="item-qty">
                    <?= (float) $_item->getQtyOrdered() ?>
                </td>
            </tr>
        
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>

Layout code is here..

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Order Items List" design_abstraction="custom">
    <update handle="sales_email_order_renderers"/>
    <update handle="sales_email_item_price"/>
    <body>
        <block class="Magento\Sales\Block\Order\Email\Items" name="items" template="Vendor_Extension::order/email/items.phtml" cacheable="false">
            <block class="Magento\Framework\View\Element\RendererList" name="sales.email.order.renderers" as="renderer.list"/>
            <block class="Magento\Sales\Block\Order\Totals" name="order_totals" template="Magento_Sales::order/totals.phtml">
                <arguments>
                    <argument name="label_properties" xsi:type="string">colspan="2"</argument>
                </arguments>
                <block class="Magento\Tax\Block\Sales\Order\Tax" name="tax" template="Magento_Tax::order/tax.phtml">
                    <action method="setIsPlaneMode">
                        <argument name="value" xsi:type="string">1</argument>
                    </action>
                </block>
            </block>
        </block>
        <block class="Magento\Framework\View\Element\Template" name="additional.product.info" template="Magento_Theme::template.phtml"/>
    </body>
</page>

2 Answers 2

0

I found my own solution And this solution works like a charm!!..

<?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 */
?>
<?php $_order = $block->getOrder() ?>
<?php $_imageHelper = $this->helper(\Magento\Catalog\Helper\Image::class); ?>
<?php if ($_order) : ?>
    <?php $_items = $_order->getAllVisibleItems(); ?>
    <table class="email-items">
        <thead>
            <tr>
                <th>
                </th>
                <th class="item-info">
                    <?= $block->escapeHtml(__('Product')) ?>
                </th>
                <th class="item-info">
                    <?= $block->escapeHtml(__('SKU')) ?>
                </th>
                <th class="item-info">
                    <?= $block->escapeHtml(__('Qty')) ?>
                </th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($_items as $_item) : ?>
            <tr>
                <td>
                    <?php if ($_item->getProduct()): ?>
                    <img src="<?= $_imageHelper->init($_item->getProduct(), 'small_image', ['type'=>'small_image'])->keepAspectRatio(true)->resize('120','120')->getUrl();?>" alt="<?= __('Product Image');?>">
                    <?php endif; ?>
                </td>
                <td>
                    <p class="product-name"><?= $block->escapeHtml($_item->getName()) ?></p>
                    <?php
                    $productType = $_item->getProductType();
                    $options = $_item->getProductOptions();
                    
                    // Display configurable product options
                    if ($productType == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE && isset($options['attributes_info'])): ?>
                        <ul>
                            <?php foreach ($options['attributes_info'] as $attribute): ?>
                                <li><strong><?= $block->escapeHtml($attribute['label']) ?>:</strong> <?= $block->escapeHtml($attribute['value']) ?></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                    <?php if ($productType == \Magento\Bundle\Model\Product\Type::TYPE_CODE && isset($options['bundle_options'])): ?>
                        <ul>
                            <?php foreach ($options['bundle_options'] as $bundleOption): ?>
                                <li><strong><?= $block->escapeHtml($bundleOption['label']) ?>:</strong>
                                    <ul>
                                        <?php foreach ($bundleOption['value'] as $bundleItem): ?>
                                            <li><?= $block->escapeHtml($bundleItem['title']) ?> <strong>QTY:</strong>  <?= (float)$bundleItem['qty'] ?></li>
                                        <?php endforeach; ?>
                                    </ul>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </td>
                <td>
                    <p class="product-sku"><?= $block->escapeHtml($_item->getSku()) ?></p>
                </td>
                <td class="item-qty">
                    <?= (float) $_item->getQtyOrdered() ?>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>
0

@Keval Chauhan

Hey,

  1. Create a custom block to fetch bundle product options.

  2. Use the Block in Email Template

    {{block class="YourVendor\YourModule\Block\BundleOptions" product_id=$product.id template="YourVendor_YourModule::bundle_options.phtml"}}

  3. Create the Template File

    getBundleOptions($block->getData('product_id')); ?>
     <ul>
         <?php foreach ($options as $option): ?>
             <li>
                 <strong><?php echo $option['label']; ?>:</strong>
                 <ul>
                     <?php foreach ($option['values'] as $value): ?>
                         <li><?php echo $value; ?></li>
                     <?php endforeach; ?>
                 </ul>
             </li>
         <?php endforeach; ?>
     </ul>
    

Thank You!

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.