4 Steps to add Custom Tab in Customer Account in Magento 2
In this article, I will introduce how to add Custom Tab in Customer Account in Magento 2. A detailed and simple guide is coming to know.
Regarding the default setting of Magento 2, customers will be redirected automatically to their account dashboard after signing up or logging in. In this dashboard, there are various tabs such as My Account, My Orders, My Wishlist.
However, in some particular situations, to enhance customers experience while shopping, several features like checking demo products, requesting refunds, or other custom requirements need to be added to customer account dashboard. Sadly, these features are not being offered in the default Magento 2. Therefore, in today’s post, I will show you 4 simple stages to add a custom tab in customers account.
How to add Custom Tab in Customer Account in 4 steps
- Step 1: Create Customer Account Layout
- Step 2: Create Index Layout
- Step 3: Create Index Page
- Step 4: Create Template File
Step 1: Create Customer Account Layout
Firstly, you will need to create the file customer_account.xml
in the path Mageplaza/Module/view/frontend/layout
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="customer_account_navigation">
<block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-custom">
<arguments>
<argument name="path" xsi:type="string">routename/customer/index</argument>
<argument name="label" xsi:type="string">My Custom Tab</argument>
</arguments>
</block>
</referenceBlock>
</body>
</page>
Step 2: Create Index Layout
After creating the above file, also in the path Mageplaza/Module/view/frontend/layout
, create routename_customer_index.xml
which includes the below code:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<body>
<referenceBlock name="page.main.title">
<action method="setPageTitle">
<argument translate="true" name="title" xsi:type="string">My Custom Tab</argument>
</action>
</referenceBlock>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="my_tab" template="Vendor_Module::mytab.phtml">
</block>
</referenceContainer>
</body>
</page>
Step 3: Create Index Page
Then go to Mageplaza/Module/Controller/Customer
, and create Index.php
<?php
namespace Mageplaza/Module\Controller\Customer;
class Index extends \Magento\Framework\App\Action\Action {
public function execute() {
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}
?>
Step 4: Create Template File
Finally, it’s easy to add custom tab in customer account dashboard, you only need follow the path Mageplaza/Module/view/frontend/templates
and create mytab.phtml
.
<?php
// Add Some Code Here for design
?>
<span> My Custom Tab.. </span>
Stick to above easy-to-follow steps to successfully add a Custom Tab in Customer account. After you finish all of the steps, don’t forget to double check the results or if there something is off the right track.
Conclusion
That’s the tutorial about how to add a Custom Tab in Customer account dashboard in Magento 2. I hope this article will be helpful for to successfully add a Custom Tab in your Customer Accounts. Let us know your opinion in the comment section and if you have any difficuties following this instruction, also tell us to get support from our certified developers.Thanks for reading!
- How to create a simple Hello World module for Magento 2
- Magento 2 Block Template Ultimate Guides
- How to Create Module in Magento 2
- How to Create Controller in Magento 2
- How to create CRUD Models in Magento 2
- How to Create Magento 2 Block, Layout and Templates
- Configuration - System.xml
- How To Create Admin Menu In Magento 2
- Admin ACL
- Admin Grid