3

How to add this functionality any one can help me

enter image description here

0
5

Edit "app/design/frontend/Vendor/Theme_Name/Magento_Search/templates/form.mini.phtml" file and add your select field after the input field like below -

<div class="control">
    <input id="search"
       data-mage-init='{"quickSearch":{
            "formSelector":"#search_mini_form",
            "url":"<?= $block->escapeUrl($helper->getSuggestUrl())?>",
            "destinationSelector":"#search_autocomplete"}
       }'
       type="text"
       name="<?= $block->escapeHtmlAttr($helper->getQueryParamName()) ?>"
       value="<?= $block->escapeHtmlAttr($helper->getEscapedQueryText()) ?>"
       placeholder="<?= $block->escapeHtmlAttr(__('Search here...')) ?>"
       class="input-text"
       maxlength="<?= $block->escapeHtmlAttr($helper->getMaxQueryLength()) ?>"
       role="combobox"
       aria-haspopup="false"
       aria-autocomplete="both"
       autocomplete="off"
       aria-expanded="false"/>
    <select class="search-type" id="searchType" name="type">
        <option value="es" <?php if(isset($_GET['type']) && $_GET['type']=='es') echo "selected";?>>Entire Site</option>
        <option value="sv" <?php if(isset($_GET['type']) && $_GET['type']=='sv') echo "selected";?>>Selected Vehicle</option>
        <option value="pn" <?php if(isset($_GET['type']) && $_GET['type']=='pn') echo "selected";?>>Part number</option>
    </select>
    <div id="search_autocomplete" class="search-autocomplete"></div>
    <?= $block->getChildHtml() ?>
</div>

After that overwrite Magento\CatalogSearch\Model\Layer\Search\Plugin\CollectionFilter file and add like filter to the collection like below-

$query = $this->queryFactory->get();
if (!$query->isQueryTextShort()) {
    $collection->addSearchFilter($query->getQueryText());
}
$type = $this->request->getParam('type');
if ($type=='sv') {
    $collection->addAttributeToFilter(
        'sku',
        array(
            array('like' => '% '.$query->getQueryText().' %'), //spaces on each side
            array('like' => '% '.$query->getQueryText()), //space before and ends with $needle
            array('like' => $query->getQueryText().' %') // starts with needle and space after
        )
    );
}

It may not be a great solution but it will do the work.

1
  • Never mind sir you worked so hard in this, i thank you for this, i told you by doing this that whether
    – DK Thakur
    Feb 4 '20 at 11:58

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.