Changing Sort Order to Name in Magento Search Results
Introduction
Magento is a powerful e-commerce platform that allows you to manage your online store effectively. However, sometimes we need to customize certain aspects of the platform to meet specific business requirements. One such requirement could be changing the default sort order for search results from relevance to product name.
In this guide, I’ll walk you through the steps required to modify the sort order for Magento 2 search results.
Understanding Default Sort Order
By default, when a customer searches for products in your store, Magento uses an algorithm to determine which items should appear at the top of the results page. This is based on factors such as product name, description, and category associations.
The default sorting options are:
- Relevance: Sorts products by their relevance to the search query.
- Popularity: Sorts products based on views, sales, or other popularity metrics.
- Price (Low to High): Sorts products in ascending order of price.
- Price (High to Low): Sorts products in descending order of price.
Changing Sort Order to Name
To change the sort order for search results to display by product name, we need to make modifications to the Magento catalog search. Here’s how:
Step 1: Override the Search Result Template
The first step is to override the default search result template with your custom template. This allows us to modify the sorting logic.
Navigate to your theme folder (usually app/design/frontend/{Vendor}/{Theme}) and create a new folder called Magento_Search if it doesn’t exist already.
Within this folder, copy the file result.phtml from the original Magento search module (vendor/magento/module-search/view/frontend/templates/result.phtml) to your custom theme folder.
Step 2: Modify the Product Sorting Logic
Open the copied result.phtml file in a text editor. You’ll see lines of code responsible for the sorting logic:
<ol id="search-results" class="products list items product-items">
<?php foreach ($collection->getItems() as $product): ?>
<li class="item item<?= /* @escapeNotVerified */ $i++ % 2 ? ' first' : '' ?> products-item">
<?= /* @escapeNotVerified */ $block->getProductListHtml($product, $viewMode, $templateType) ?>
</li>
<?php endforeach; ?>
</ol>
To change the sorting to product name:
- Find the line
$collection = $this->searchHelper->getProductsBySearchQuery($_query); - Replace it with this updated code:
$collection = $productCollectionFactory->create();
$searchCriteria = $this->searchHelper->getRequest()->getParams();
$searchCriteria->setCurrentPage(1);
$searchResults = $this->searchService->search($searchCriteria)->getItems();
foreach ($searchResults as $result) {
$productId = $result->getSku();
$product = $this->productRepository->getById($productId);
$collection->addItem($product);
}
$collection->setOrder('name', 'asc');
Step 3: Compile and Deploy Changes
After making the changes, save the file. Then, compile your Magento code using the command bin/magento setup:static-content:deploy -f.
Finally, clear the cache with bin/magento cache:clean or bin/magento cache:flush.
Conclusion
Changing the sort order for search results in Magento requires a combination of overriding templates and modifying the sorting logic. By following this guide, you’ve learned how to display products sorted by name within your search results page.
Remember that while this method works for simple stores, complex setups may require more advanced solutions involving plugins or custom modules. Always ensure you understand the potential impacts before proceeding with major code changes.
For further guidance on Magento development and e-commerce best practices, check out our comprehensive resources at Laravel Company. We offer a wide range of services to help you maximize your online business success.