2

Top menu adding a button

I am new in magento. How can I add an html button on that location?

2 Answers 2

0

Do not know it has to be a button but you can add an hyperlink this way:

in app\code\vendor\module\etc\di.xml

<type name="Magento\Theme\Block\Html\Topmenu">
        <plugin name="add_contact_us_menu" type="vendor\module\Plugin\TopMenu" sortOrder="1" />
    </type>

in app\code\vendor\module\Plugin\TopMenu.php

<?php 
namespace vendor\module\Plugin;
use Magento\Framework\Data\Tree\NodeFactory;
class TopMenu
{
    protected $nodeFactory;
    protected $_storeManager;
    protected $_urlBuilder;

    public function __construct(
        NodeFactory $nodeFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
        $this->nodeFactory = $nodeFactory;
        $this->_storeManager = $storeManager;
        $this->_urlBuilder = $urlBuilder;
    }
    public function beforeGetHtml(
        \Magento\Theme\Block\Html\Topmenu $subject,
        $outermostClass = '',
        $childrenWrapClass = '',
        $limit = 0
    ) 
    {

        
            $node = $this->nodeFactory->create(
                [
                    'data' => [
                          'name' => __('Node title'),
                          'id' => 'cutom-page',
                          'url' =>  your url here,
                          'has_active' => false,
                          'is_active' => false 
                      ],
                     'idField' => 'id',
                     'tree' => $subject->getMenu()->getTree(),

                  ]
        
            $subject->getMenu()->addChild($node);
                
    }
}
0
            'data' => [
                'name' => __('Contact Us'),
                'id' => 'cutom-page',
                'url' => 'http://magento244.local/what-is-new.html',
                      'has_active' => false,
                      'is_active' => false
                  ],
                 'idField' => 'id',
                 'tree' => $subject->getMenu()->getTree(),
              ]);

        $subject->getMenu()->addChild($node);
            

Thank you @Joao71 This code is working but you missed );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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