custom atributes magento 2 my account page

How to Add Custom Attributes in Account Page? – Magento 2 Tutorial

Custom Attributes help store owners collect more information about the customer and offer a better customer experience. When you add custom attributes in the My Account Page, it even helps make the overall shopping experience better as the basic details are filled in. In today’s article, we will help you with a detailed guide on how to add custom attributes to the My Account page in Magento 2.

Also, if you are looking for a smart and short way to add custom attributes, MageDelight’s Magento 2 Extensions catalog comes with an extension called Customer Attributes. Magento 2 Customer Attributes Extension facilitates the store owners to create a variety of customer attributes by displaying additional attribute fields at the various places, not just My Account Page. This type of data can be collected to form information out of it about customers’ habits and offer a personalized experience.

Step 1 – Create Layout File

file: app/code/MageDelight/CustomerAttribute/view/frontend/layout/customer_account_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Magento\Customer\Block\Account\Dashboard\Info" name="customer_account_dashboard_info" as="info" template="Magento_Customer::account/dashboard/info.phtml" cacheable="false">
                <container name="customer.account.dashboard.info.extra">
                    <block class="Magento\Customer\Block\Account\Dashboard\Info" name="customer.account.dashboard.info.extra.magento.username" template="MageDelight_CustomerAttribute::account/dashboard/info.phtml"/>
                </container>
            </block>
        </referenceContainer>
    </body>
</page>

Step 2 – Create Template File

file: app/code/MageDelight/CustomerAttribute/view/frontend/templates/account/dashboard/info.phtml.

<p>
     <?= $block->escapeHtml($block->getCustomer()->getCustomAttribute('magento_username')->getValue()) ?><br>
</p>

There you go! This is how you can add custom attributes to your Magento 2 store.

We hope this guide helps you successfully add custom attributes and collect additional information from your customers. If you face any issues, our team of Magento experts will be happy to help. We provide end-to-end Magento development services, which will help you develop a seamless experience in your store.

Also read: How to Create Product Attributes in Magento 2?

Tags