1

How to add a new item in the dropdown of the customer menu on the frontend? enter image description here

I want to add a new item below my account.

7
  • Can you please share, what kind of Link you want to add in that section?
    – Sumit
    Aug 29 '19 at 10:07
  • @Sumit your solution is working for me. Thanks. How i change position ?
    – Ashar Riaz
    Aug 29 '19 at 11:38
  • Good to hear, you can change it with the before and after attributes given in default.xml file
    – Sumit
    Aug 29 '19 at 11:40
  • Thanks. The only issue is that it is not coming in mobile.
    – Ashar Riaz
    Aug 29 '19 at 12:07
  • Are you using a different theme for the mobile?
    – Sumit
    Aug 29 '19 at 12:10
1

You can add it by following the below steps

Create default.xml file in your custom module with below content

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="top.links">
            <block class="Vendor\Module\Block\Link" name="custom-link" after="my-account-link"/>
        </referenceBlock>
    </body>
</page>

Create Link.php with the below content

<?php

namespace Vendor\Module\Block;

/**
 * Customer account dropdown link
 */
class Link extends \Magento\Framework\View\Element\Html\Link
{
    /**
     * @var string
     */
    protected $_template = 'Vendor_Module::link.phtml';

    /**
     * @return string
     */
    public function getHref()
    {
        return $this->getUrl('custom-link');
    }

    /**
     * @return \Magento\Framework\Phrase
     */
    public function getLabel()
    {
        return __('Custom Link');
    }
}

Create link.phtml with below content

<?php
// @codingStandardsIgnoreFile

/* @var $block \Vendor\Module\Block\Link */
?>
<li class="link custom wishlist" data-bind="scope: 'credit'">
    <a <?= $block->getLinkAttributes() ?>><?= $block->escapeHtml($block->getLabel()) ?>
        <span>Custom Link</span>
    </a>
</li>

Hope it helps!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.