Magento 2.1 – How to Display a Search Bar in Website Header

catalogsearchmagento-2.1search

I am working on a Magento 2.1.7 shop. To achieve this, I have created a child-theme of Magento Blank.

I have moved the top links at the right of the logo:

<move element="top.links" destination="header-wrapper" after="logo" />

I want to display the search bar (it does not display anywhere) above the top links (like the screenshot indicates).

enter image description here

For this purpose, I wrote the line below, in my default.xml file:

<move element="top.search" destination="header-wrapper" before="links" />

The result is that the search bar not only does not display above the top links but it does not display at all.

There is alse a app/design/frontend/Company_Name/Theme_Name/Magento_Search/layout/default.xml file with this code:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="top.search" destination="navbar-collapse" after="navigation.sections" />
        <referenceBlock name="advanced-search-link" remove="true" />
    </body>
</page>

As you can see, on the website's homepage, the search box does not appear anywhere.

  1. What am I doing wrong?
  2. Could the search feature be disabled from admin?
  3. Can I display the search bar inside the top links <ul> element?

Best Answer

I added the following code sequence in my custom theme's default.xml file:

<container name="header.main.search" htmlTag="div" htmlClass="header-search" after="-">
<block class="Magento\Framework\View\Element\Template" template="Magento_Search::form.mini.phtml" />
</container>
<move element="header.main.search" destination="header-wrapper" after="logo" />
<move element="top.links" destination="header-wrapper" after="header.main.search" />

The code above renders the following HTML in the page source:

<div class="header content">
  <strong class="logo">
   <img src="https://dev3.pro-detailing.ro/pub/static/version1516374892/frontend/ProDetailing/v1/en_US/images/logo.png" alt="Magento Commerce" width="263" height="85">
  </strong>
  <div class="header-search">
    <form class="navbar-form navbar-right" role="search" id="search_mini_form" action="https://dev3.pro-detailing.ro/catalogsearch/result/" method="get">
      <div class="form-group">
        <input type="text" name="q" value="" placeholder="Search entire store here..." class="form-control" maxlength="128" role="combobox" aria-haspopup="false" aria-autocomplete="both" autocomplete="off">
      </div>
      <button type="submit" class="btn btn-default" disabled="">Search</button>
      <div id="search_autocomplete" class="search-autocomplete" style="display: none;"></div>
    </form>
  </div>
  <ul class="list-inline clearfix header links">
    <li><a href="https://dev3.pro-detailing.ro/customer/account/">My Account</a></li>
    <li class="authorization-link" data-label="or">
      <a href="https://dev3.pro-detailing.ro/customer/account/login/">
      Sign In</a>
    </li>
  </ul>
</div>

This is what I was looking for.

Related Topic