{"id":1971,"date":"2021-12-17T09:21:05","date_gmt":"2021-12-17T09:21:05","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/dynamically-add-link-to-customer-account-navigation-in-magento-2\/"},"modified":"2025-07-16T16:56:04","modified_gmt":"2025-07-16T11:26:04","slug":"dynamically-add-link-to-customer-account-navigation-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/dynamically-add-link-to-customer-account-navigation-in-magento-2\/","title":{"rendered":"How to Dynamically Add Link to Customer Account Navigation in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2\u00a0eCommerce CMS allows customizing the default features based on the business requirements. Magento offers a rule-based condition field using which the admin can easily configure the condition-based features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">While creating a navigation link on my account dashboard, you need to create a customer file as static. But, at times, you need to change it to dynamic. This is not possible in default Magento.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an instance, in Multi-vendor extension, there are multiple types of sellers such as retailers, wholesalers, general. So, every customer group requires different functionalities that might be unnecessary for others. Custom options are needed for the sellers to add a new product. In such a case, it displays such options to sellers only. Whereas, for the customers, such an option is not visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, you can easily create a custom link dynamically using the code provided here. I have provided a complete solution to<em><strong>&nbsp;dynamically add link to customer account navigation in Magento 2&nbsp;<\/strong><\/em>below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to&nbsp;Dynamically Add Link to Customer Account Navigation in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>customer_account.xml<\/strong><\/em>&nbsp;file under&nbsp;<strong>app\/code\/Vendor\/Module\/view\/frontend\/layout<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceBlock name=\"customer_account_navigation\">\n            &lt;block class=\"Vendor\\Module\\Block\\Customer\\DynamicLink\" name=\"customer-navigation-dynamic-link\" after=\"-\">\n                &lt;arguments>\n                    &lt;argument name=\"label\" xsi:type=\"string\">Dynamic Link&lt;\/argument>\n                    &lt;argument name=\"path\" xsi:type=\"string\">customer\/dynamicLink\/index&lt;\/argument>\n                &lt;\/arguments>\n            &lt;\/block>\n        &lt;\/referenceBlock>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<em><strong>DynamicLink.php<\/strong><\/em>&nbsp;file under&nbsp;<strong>app\/code\/Vendor\/Module\/Block\/Customer\/<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Module\\Block\\Customer;\n\nuse Magento\\Framework\\View\\Element\\Html\\Link\\Current;\nuse Magento\\Framework\\View\\Element\\Template\\Context;\nuse Magento\\Framework\\App\\DefaultPathInterface;\nuse Magento\\Customer\\Model\\Session;\n\nclass DynamicLink extends Current\n{\n    protected $customerSession;\n\n    public function __construct(\n        Context $context,\n        DefaultPathInterface $defaultPath,\n        Session $customerSession,\n        array $data = []\n     ) {\n         $this->customerSession = $customerSession;\n         parent::__construct($context, $defaultPath, $data);\n     }\n\n    protected function _toHtml()\n    {    \n        $responseHtml = null;\n        if($this->customerSession->isLoggedIn()) {\n                \/\/ here you can put your custom conditions\n                $responseHtml = parent::_toHtml();\n        }\n        return $responseHtml;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, I\u2019d be grateful if you could share the solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2\u00a0eCommerce CMS allows customizing the default features based on the business requirements. Magento offers a rule-based condition field using which the admin can easily&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1971","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1971"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1971\/revisions"}],"predecessor-version":[{"id":18000,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1971\/revisions\/18000"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}