Hello Community,
Our requirement is we have to remove "Relevance" option from product list page and catalog search result page. we did that from "Sorter.phtml we checked if "Relevance" option is there then do not display. . code is as follows.
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ // @codingStandardsIgnoreFile ?> <?php /** * Product list toolbar * * @var $block \Magento\Catalog\Block\Product\ProductList\Toolbar */ use Magento\Catalog\Model\Product\ProductList\Toolbar; ?> <div class="toolbar-sorter sorter"> <label class="sorter-label" for="sorter"><?= /* @escapeNotVerified */ __('Sort By') ?></label> <select id="sorter" data-role="sorter" class="sorter-options"> <?php foreach ($block->getAvailableOrders() as $_key => $_order): ?> <?php if ($_order != 'Relevance') : ?> <option value="<?= /* @escapeNotVerified */ $_key ?>" <?php if ($block->isOrderCurrent($_key)): ?> selected="selected" <?php endif; ?> > <?= $block->escapeHtml(__($_order)) ?> </option> <?php endif; ?> <?php endforeach; ?> </select> <?php if ($block->getCurrentDirection() == 'desc') :?> <a title="<?= $block->escapeHtmlAttr(__('Set Ascending Direction')) ?>" href="#" class="action sorter-action sort-desc" data-role="direction-switcher" data-value="asc"> <span><?= $block->escapeHtml(__('Set Ascending Direction')) ?></span> </a> <?php else :?> <a title="<?= $block->escapeHtmlAttr(__('Set Descending Direction')) ?>" href="#" class="action sorter-action sort-asc" data-role="direction-switcher" data-value="desc"> <span><?= $block->escapeHtml(__('Set Descending Direction')) ?></span> </a> <?php endif; ?> </div>
With above code we can remove "Relevance" option from Product list page and search result page. Now Product List page working properly.
The issues comes on search result page. As we removed Relevance Option (from dropdown) only Name option is there. but Magento2 by default uses order "Relevance" for search result. so when I search by product title, it displays on first place but sorting is not working. (Because there is no any option available to change sort by. only Name option is there and by default result is based on Relevance) . Hence we decide to override block function "setListOrders()" from Magento\CatalogSearch\Block\Result
Now the code looks like as follows.
class Result extends \Magento\CatalogSearch\Block\Result { /** * Set search available list orders * * @return $this */ public function setListOrders() { $category = $this->catalogLayer->getCurrentCategory(); /* @var $category \Magento\Catalog\Model\Category */ $availableOrders = $category->getAvailableSortByOptions(); unset($availableOrders['position']); $availableOrders['relevance'] = __('Relevance'); $this->getListBlock()->setAvailableOrders( $availableOrders )->setDefaultDirection( 'asc' )->setDefaultSortBy( 'name' ); return $this; } }
Now with this. Sorting is working but The product which we searched by its title not coming on first place. How to resolve this issue ?
Is there any solution for this ? Please help.
Thanks all in advanced !
Hi @maratheprash
Have you tried the below stack solution?
https://magento.stackexchange.com/a/253624
Might be it will help you!