Displaying “My Account” Link After Successful Login in Magento 2
In Magento 2, displaying the “My Account” link after a successful customer login requires modifying the default header template and adding conditional logic for authentication status. Here’s a step-by-step guide to achieve this:
1. Modify the Top Links Block
First, you need to override the vendor/magento/module-theme/view/frontend/templates/html/toplinks.phtml file in your theme.
Theme Path: app/design/frontend/<Vendor>/<Theme>/Magento_Theme/templates/html/toplinks.phtml
Replace the contents of this file with the following code:
<?php $customerName = '';
if ($block->getIsLoggedIn()) {
$customerName = $block->escapeHtml($block->getCustomer()->getFirstname());
}
$authorizationLinks = [
'account_link' => $block->getUrl('customer/account/index'),
];
foreach ($authorizationLinks as $_key => $_url) {
if ($_key == 'account') {
$html = '<span class="account">';
$html .= '<a href="' . $_url .'" title="' . $block->escapeHtml($this->getLabel($_key)) . '">' . $customerName . '</a>';
$html .= '</span>';
}
}
echo $html;
This code checks if a customer is logged in and escapes the first name for display. It then generates an “account” link with the current customer’s first name.
2. Update the Layout XML
Next, update your theme’s app/design/frontend/<Vendor>/<Theme>/Magento_Theme/layout/default.xml file to include the following:
<referenceBlock name="top.links">
<arguments>
<argument name="sort" xsi:type="array">
<item name="sort_order" xsi:type="string">10</item>
</argument>
</arguments>
</referenceBlock>
This ensures that the modified top links are displayed first.
3. Flush the Magento Cache
After making these changes, make sure to flush your Magento cache:
php bin/magento cache:flush
4. Verify the Changes
Now, log out of your Magento admin panel and clear your browser’s cookies. Then, access your storefront and log in as a customer.
Upon successful login, you should see “My Account” displayed in the header with the current customerâs first name:

Conclusion
In this tutorial, we demonstrated how to display the “My Account” link after a successful customer login in Magento 2. By overriding the default top links block and adding conditional logic for authentication status, you can provide a seamless user experience.
Remember that it’s essential to keep your theme up-to-date with the latest Magento versions to ensure compatibility and security. If you’re looking for professional development services or a comprehensive guide on Magento customization, consider visiting https://laravelcompany.com for expert insights and solutions tailored to your business needs.