Add custom attribute in order email templates - Magento

Add custom attribute in order email templates - Magento

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add custom attribute in order email templates - Magento

Displaying Custom Company Name Attribute in Magento Order Email Templates

As an experienced e-commerce developer, I understand your frustration when custom attributes don’t automatically appear in email templates. In this guide, we’ll walk through the steps to display your ‘Company Name’ attribute in all types of Magento order emails - invoices, shipments, and credit memos.

Step 1: Extend the Core Sales Model

First, you need to extend Magento’s core Magento\Sales\Model\Order model. Create a new file at app/code/LaravelCompany/Sales/etc/di.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order">
        <arguments>
            <argument name="customAttributeCodes" xsi:type="array">
                <item name="company_name" xsi:type="string">company_name</item>
            </argument>
        </arguments>
    </type>
</config>

This tells Magento to load the ‘Company Name’ attribute whenever an order object is created or loaded.

Step 2: Add Custom Company Name Field

Next, you need to add a field for the ‘Company Name’ in your sales emails. Override Magento\Sales\view\frontend\templates\order\invoice.phtml and shipment.phtml:

<div class="attribute-line">
    <span class="label"><?= /* @escapeNotVerified */ __('Company') ?>:</span>
    <span><?= $this->helper('Magento\Sales\Helper\Data')->getAttributeText($item, 'company_name') ?></span>
</div>

The getAttributeText() helper retrieves the value of a custom attribute from an order item.

Step 3: Add Custom Variable for Credit Memos

For credit memos, you need to create a new variable to hold the company name. In your module configuration file (e.g., app/code/LaravelCompany/Sales/etc/config.xml), add:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/config.xsd">
    <sales>
        <creditmemo>
            <variables>
                <company_name translate="label"></company_name>
            </variables>
        </creditmemo>
    </sales>
</config>

Then, in your credit memo template app/code/LaravelCompany/Sales/view/frontend/templates/creditmemo.phtml:

<div class="attribute-line">
    <span class="label"><?= /* @escapeNotVerified */ __('Company') ?>:</span>
    <span><?= $this->getMessageBlock() ?></span>
</div>

Step 4: Add Company Name to the Email

Finally, you need to add the ‘Company Name’ variable to your email templates. In app/code/LaravelCompany/Sales/etc/email_templates.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <global>
        <template id="sales_new_order_template" label="New Order Notification" file="sales/order/creditmemo/new.phtml" type="html" area="frontend">
            <variable name="company_name"></variable>
        </template>
    </global>
</config>

This tells Magento to include the ‘company_name’ variable in all sales email templates.

Conclusion

Displaying a custom company name attribute in Magento order emails requires extending core models, adding template variables, and overriding existing templates. By following these steps, you can ensure that your customers see their company names clearly in all types of sales email communications.

Remember to clear your cache after making changes. If you’re new to Magento development, consider exploring our Magento Development Services for expert assistance with customizations like these.

The key is to understand how Magento’s template and variable systems work, which I’ve tried to explain in a clear and practical way.

Themes Configuration Email Orders

Our network