2

Top menu adding a button

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

2 Answers2

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-&gt;nodeFactory = $nodeFactory;
    $this-&gt;_storeManager = $storeManager;
    $this-&gt;_urlBuilder = $urlBuilder;
}
public function beforeGetHtml(
    \Magento\Theme\Block\Html\Topmenu $subject,
    $outermostClass = '',
    $childrenWrapClass = '',
    $limit = 0
) 
{


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

              ]

        $subject-&gt;getMenu()-&gt;addChild($node);

}

}

Joao71
  • 1,197
  • 6
  • 16
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 );