In Magento 2, when customer login to the website, he is given the My Account option on the right side. Clicking on it opens the Myaccount section. In this blog, we are going to explain how to add additional tabs to that section.
Why is it important to add custom tabs? Because to get some additional information of customers. To show a demo of a new product. A custom tab is created for another such additional requirement.
It is very easy to make a custom tab see this step:-
⇒ You have to create the file customer_account.xml in this path Vendorname/Modulename/view/frontend/layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version=“1.0” encoding=“UTF-8”?> <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=“mobile-number-tab-link” cacheable=“false”> <arguments> <argument name=“path” xsi:type=“string”> routename/customer/index</argument> <argument name=“label” xsi:type=“string”>Custom Tab Name</argument> </arguments> </block> </referenceBlock> </body> </page> |
⇒ You have to create file routename _customer_index.xml in this path Vendorname/Modulename/view/frontend/layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version=“1.0” encoding=“UTF-8”?> <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”> Custom Tab Name </argument> </action> </referenceBlock> <referenceContainer name=“content”> <block class=“Magento\Framework\View\Element\Template” name=“custom-tab-link” template=” Vendorname_Modulename::customer/account/customtab.phtml” cacheable=“false”/> </referenceContainer> </body> </page> |
⇒ You have to create file routes.xml in this path Vendorname/Modulename/etc/frontend
1 2 3 4 5 6 7 8 |
<?xml version=“1.0” ?> <config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:framework:App/etc/routes.xsd”> <router id=“standard”> <route frontName=“custaccounttab” id=“custaccounttab”> <module name=” Vendorname_Modulename “/> </route> </router> </config> |
⇒ You have to create file Index.php in this path Vendorname/Modulename/Controller/Customer
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace Vendorname\Vendorname \Controller\Customer; class Index extends \Magento\Framework\App\Action\Action { public function execute() { $this->_view->loadLayout(); $this->_view->renderLayout(); } } |
⇒ You have to create file customtab.phtml in this path Vendorname/Modulename/ view/frontend/templates/customer/account
1 2 3 4 |
<?php // Add your tab content here. echo “This is a custom tab “; |
Then you should do this commend php -dmemory_limit=2G bin/magento cache:flush and see the custom tab has successfully landed in the myAccount section. i hope you have to understand this blog.if you have any questions let me know in comment section
Thank You :〉