How can I change the default order by value and direction for search results listing page?
The default is set to Relevance but I want to change this permanently to something else i.e. Price for example.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityTry a below code. Tested in M2.3.
This will change the sorting option from Relevance to Price only for Search Result Page.
You can change the $defaultSortBy
and $directionToSet
as per your need.
File - Custom/SortBy/etc/frontend/di.xml
<type name="Magento\CatalogSearch\Block\Result">
<plugin name="Custom_SortBy::setPriceSortBy" type="Custom\SortBy\Plugin\Search\Block\Result" sortOrder="100"/>
</type>
File - Custom/SortBy/Plugin/Search/Block/Result.php
<?php
namespace Custom\SortBy\Plugin\Search\Block;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\Element\Template\Context as Context;
use Magento\Catalog\Model\Layer\Resolver as LayerResolver;
use Magento\Framework\View\Element\Template as ViewTemplate;
use Magento\Search\Model\QueryFactory;
class Result extends ViewTemplate
{
public function __construct(
Context $context,
LayerResolver $layerResolver,
QueryFactory $queryFactory,
array $data =[]
)
{
$this->_catalogLayer = $layerResolver->get();
$this->_queryFactory = $queryFactory;
parent::__construct($context, $data);
}
public function afterSetListOrders() {
$query = $this->_queryFactory->get();
$queryText = $query->getQueryText();
$category = $this->_catalogLayer->getCurrentCategory();
$availableOrders = $category->getAvailableSortByOptions();
unset($availableOrders['position']);
$availableOrders['relevance'] = __('Relevance');
$directionToSet = 'desc';
$defaultSortBy = 'price';
if($queryText == 'ABC') {
$directionToSet = 'asc';
} elseif($queryText == 'DEF') {
$directionToSet = 'desc';
} else {
$directionToSet = 'desc';
}
$this->getLayout()->getBlock('search_result_list')->setAvailableOrders(
$availableOrders
)->setDefaultDirection(
$directionToSet
)->setDefaultSortBy(
$defaultSortBy
);
return $this;
}
}
File - Custom/SortBy/etc/module.xml if required
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Custom_SortBy">
<sequence>
<module name="Magento_CatalogSearch"/>
</sequence>
</module>
</config>
As mentioned by Martin you can follow those steps but that'll not make any impact on the search result page.
Here's a fix that helped me to achieve the same.
Inside
form.mini.phtml at location /var/www/html/magento2/vendor/magento/module-search/view/frontend/templates
Just add below lines just after the form tag starts.
<input type="hidden" name="product_list_order" value="price" />
<input type="hidden" name="product_list_dir" value="asc" />
Clear the cache and run setup:upgrade and you're good to go.
In case, if you don't want to modify vendor file, you can just create your own module and just extend the same file.
For only Search result page you need to override Magento\CatalogSearch\Block\Result file as below:
1.Edit your module's di.xml file, add below preference,
<preference for="Magento\CatalogSearch\Block\Result" type="Vendor\Module\Block\Search\Result" />
2. Add New block Result as mention above:
namespace Vendor\Module\Block\Search;
class Result extends \Magento\CatalogSearch\Block\Result
{
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(
'desc'
)->setDefaultSortBy(
'price'
);
// 'price' you can set any attribute here ex.: name, price, any custom attribute
return $this;
}
}
Let me know if not working.
If you mean the order by which the product's are outputted on the product listing page(Category View) then there is a configuration in the backend of Magento 2 to change that. The Config is Under:
"Stores" -> "Configuration" -> "Catalog" -> "Catalog" -> "Storefront" -> "Product Listing Sort by"
Not sure if this applied to the Search Result page though.
For this, you need to override the Magento core block file. Step 1: create di.xml file with below code in etc folder.
Step 2: Create Result.php file in Vendor\Module\Block\Search folder. Add below code in it. catalogLayer->getCurrentCategory(); /* @var $category \Magento\Catalog\Model\Category */ $availableOrders = $category->getAvailableSortByOptions(); unset($availableOrders['position']); $availableOrders['relevance'] = __('Relevance'); $this->getListBlock()->setAvailableOrders( $availableOrders )->setDefaultDirection( 'desc' )->setDefaultSortBy( 'price' ); return $this; } } ?>Or
If you are not familiar with Magento coding then I suggest you use Solwin Advanced Sorting Magento 2 extension.
Advanced Sorting Magento 2 extension will add custom sorting options to your store to make the product search process easier for customers.
Refer the link below: https://www.solwininfotech.com/product/magento-2-extensions/advanced-sorting-magento-2/