Magento: Modifying Customer Account Sidebar Navigation Links
Identifying the Responsible File
In Magento, the sidebar navigation links on a customer’s account dashboard are controlled primarily by the customer_account_dashboard layout XML file. This file resides within the core template package of your Magento installation.
The exact location may vary depending on your version and theme customizations, but in vanilla Magento 2, you can typically find it here:
app/design/frontend/Magento/Customtheme/Magento_Customer/layout/customer_account_dashboard.xml
Understanding the XML Structure
The customer_account_dashboard XML file defines the layout for the customer account dashboard page. It includes various blocks that are placed within a container.
To modify the sidebar navigation, you’re interested in the <block type="Magento\Customer\Block\Account\Dashboard"> element. This block contains the references to the sidebar links:
<block type="Magento\Customer\Block\Account\Dashboard" name="customer_account_dashboard">
<action method="setTemplate">
<argument name="template" xsi:type="string">Magento_Customer::account/dashboard.phtml</argument>
</action>
<block type="Magento\Newsletter\Block\Subscriptions" name="customer_account_newsletter_subscribe" as="customer_account_newsletter_subscribe"/>
<!-- ...other blocks... -->
</block>
Editing the Sidebar Links
To change the text of the “Newsletter Subscriptions” link to “Latest News”, you need to modify the customer_account_dashboard layout XML file and update the template referenced here.
First, let’s edit the XML:
<block type="Magento\Customer\Block\Account\Dashboard" name="customer_account_dashboard">
<action method="setTemplate">
<argument name="template" xsi:type="string">Laravel_Company::account/dashboard.phtml</argument>
</action>
<!-- ...other blocks... -->
</block>
We’ve replaced the template path with a custom one pointing to our theme.
Creating the Custom Template
Next, create a new dashboard.phtml file in your custom theme:
app/design/frontend/Laravel_Company/lara_magento/Magento_Customer/templates/account/dashboard.phtml
In this PHTML file, look for the following line of code, which renders the “Newsletter Subscriptions” link:
<?= $block->getChildHtml('customer_account_newsletter_subscribe') ?>
To change the text, modify the block rendering line like so:
<?= $block->getLayout()->createBlock(
'Magento\Customer\Block\Account\Newsletter\Subscriptions',
'laravel_newsletter_block'
)->setTitle(__('Latest News'))->toHtml() ?>
Here’s what we’ve done:
- Created a new block instance using the
createBlockmethod. - Set the title of the block to “Latest News” using the
setTitlemethod.
Other Considerations
Remember that Magento 2 is built on XML layout files, which allow for flexible and modularized design. By understanding these layouts and how blocks are referenced within them, you can easily customize various aspects of your store’s frontend.
In addition to modifying the sidebar links, you might also want to modify other elements on the customer dashboard page. The customer_account_dashboard layout XML file provides a good starting point for understanding the overall structure of this important section of your Magento site.
Conclusion
Modifying the sidebar navigation links on a customer’s account dashboard in Magento involves editing the customer_account_dashboard layout XML file and updating the corresponding PHTML template. By following these steps, you can easily change the text of links like “Newsletter Subscriptions” to more relevant or brand-specific options.
Always ensure that your customizations are properly version-controlled and tested to maintain a smooth development process. For more advanced customizations and best practices in Magento 2, I recommend exploring the resources available at Laravel Company.