How to get product attributes in email?

#1

A product has attributes, such as name, sku, and more. How to get them in an email for example when ordering, asking … There are instructions, but they are based on .phtml files - but in 2.3 I see the email templates only .html

0 Likes

#2

Yes, you’ll have to override or extend the current module that handles this by creating a custom module that pre-loads the data so that it can be injected into the template. - Which would be a phtml file. I don’t have a working example of this to share, but you should find the 2 references below useful.

Recommended reading:

0 Likes

#3

Coincidently, I’m working on something to do with Emails and I figured whilst I’m at it, that I test the Solution that I referred to in my previous reply. And with some minor tweaks it works.


1. Create the registration.php file

Each module must have this file, which tells Magento how to locate the module.

Folder/File: app/code/DigitalStartup/EmailAttributes/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'DigitalStartup_EmailAttributes',
    __DIR__
);

2. Create the composer.json file

Composer reads a composer.json file in Magento’s root directory to download third-party dependencies listed in the file.

Folder/File: app/code/DigitalStartup/EmailAttributes/composer.json

{
    "name": "digitalstartup/emailattributes",
    "description": "This module overrides core files that allows the ability to safely add additional attributes to the Invoice emails.",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        
        "magento/magento-composer-installer": "*"
    },
    "suggest": {

    },
    "type": "magento2-module",
    "version": "0.1.0",
    "license": [

    ],
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "DigitalStartup\\EmailAttributes\\": ""
        }
    },
    "extra": {
        "map": [
            [
                "*",
                "DigitalStartup/EmailAttributes"
            ]
        ]
    }
}

3. Add the component’s module.xml file

Declare the component itself by adding a module.xml file in the /etc folder of your component.

Folder/File: app/code/DigitalStartup/EmailAttributes/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="DigitalStartup_EmailAttributes" setup_version="0.1.0">
    </module>
</config>

4. Create layout file

This file declares what files need handling

Folder/File: app/code/DigitalStartup/EmailAttributes/view/frontend/layout/sales_email_order_renderers.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.renderers">
            <block class="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" as="default" template="DigitalStartup_EmailAttributes::email/items/order/default.phtml"/>
        </referenceBlock>
    </body>
</page>

5. Create override file

This is a copy of the original file, plus any changes you want to make

Folder/File: app/code/DigitalStartup/EmailAttributes/view/frontend/templates/email/items/order/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>
        <p class="color"><?= /* @escapeNotVerified */  __('Color') ?>: <?= $productcolor = $_item->getProduct()->getAttributeText('color') ?></p> <!-- NEW LINE ADDED TO DISPLAY COLOR ATTRIBUTE -->
        <?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>
</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; ?>


Result


0 Likes

#4

Parse error: syntax error, unexpected ‘version’ (T_STRING) in /app/code/Amy/InvoiceEmailPcb/registration.php on line 1

0 Likes

#5

At a glance, I think “Amy” typo’d. Remove <?xml version="1.0"?> from the top of the registration.php. It doesn’t belong there. I submitted an edit to her post.

If that doesn’t work, I’ll share my code/files in the morning.

0 Likes

#6

Well.
Now if :
/usr/local/bin/php72 bin/magento indexer:reindex
/usr/local/bin/php72 bin/magento cache:clean
/usr/local/bin/php72 bin/magento cache:flush
/usr/local/bin/php72 bin/magento setup:static-content:deploy -f
->
"
config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:framework:Module/etc/module.xsd”>

</config

"

0 Likes

#7

And in the admin panel, menus without icons are not active.

0 Likes

#8

I’ve updated my answer with a full walkthrough. Tested and works as expected on my clean install of Magento 2.3. Steps 1-3 are generic files required to build the foundation of a module. The actual magic is in Step 4-5.

0 Likes

#9

Good. It works when ordering. The question of how data can be obtained when a question about a product is not registered? For example, on each product page there is a form with the ability to ask a question about a product before purchase - how to transfer its name and SKU to an email? There are similar extensions that hide the price and you can ask a question. Those. a form that is on every page with data that the user fills in, while the product data is sent to the administrator in the email.
Example: https://landofcoder.com/magento-2-hide-price.html or https://github.com/Darshanmodi1427/Darsh_Callforprice

0 Likes