如何在订单电子邮件中添加产品的自定义属性(例如最佳食用日期)? (magento 2.3)

How to add custom attribute of products (e.g. best before date) in order email? (magento 2.3)

我有每个产品的自定义属性 'showbbd' 和 'bbd',以确定我是否要显示产品的最佳食用日期。

我想在订单电子邮件中显示 'showbbd' 设置为 true 的产品的最佳食用日期,就在 'SKU' 下方。

我正在编辑 app/code/Magento/Sales/view/frontend/templates/email/items/order/default.phtml,添加以下行,但没有成功:

<?php 
    $objectManager = Magento\Framework\App\ObjectManager::getInstance();
    $productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($_item->getSku());
    $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
    $show_bbd = $product->getAttributeText('showbbd');     
    if($show_bbd=='Yes'){
?>
        <p><b><?= /* @escapeNotVerified */  __('BBD') ?>: <?= $product-> getAttributeText('bbd'); ?></b></p>
<?php        
    }
?>

有人可以帮忙吗?

您需要覆盖 vendor/magento/module-sales/view/frontend/templates/email/items/invoice/default.phtml

您可以像下面这样创建扩展 这是我的 registration.php 文件 app/code/Jsc/InvoiceEmailJsc/registration.php

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Jsc_InvoiceEmailJsc" setup_version="1.0.0"/>
</config>

这是我的 module.xml 文件 app/code/Jsc/InvoiceEmailJsc/etc/module.xml

<?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 Creditmemo Items List" design_abstraction="custom">
    <body>
        <referenceBlock name="sales.email.order.invoice.renderers">
            <block class="Magento\Sales\Block\Order\Email\Items\DefaultItems" as="default" template="Jsc_InvoiceEmailJsc::email/items/invoice/default.phtml"/>
        </referenceBlock>
    </body>
</page>

这是我的 default.phtml app/code/Jsc/InvoiceEmailJsc/view/frontend/templates/email/items/invoice/default.phtml

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

// @codingStandardsIgnoreFile

?>
<?php $_item = $block->getItem() ?>
<?php $_order = $block->getItem()->getOrder(); ?>

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

<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>
                <?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->getOrderItem())->toHtml() ?>
        <?php endif; ?>

    <?php //Your code starts to show custom attribute value ?>
    <dl class="item-options">
        <dt><?= __('BBD') ?>:</dt>
        <dd><?php echo $product->getData('showbbd'); ?></dd>
    </dl>
    <?php //Your code ends to show custom attribute value ?>

        <?= $block->escapeHtml($_item->getDescription()) ?>
    </td>
    <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQty() * 1 ?></td>
    <td class="item-price">
        <?= /* @escapeNotVerified */  $block->getItemPrice($_item) ?>
    </td>
</tr>