Customizing Magento’s “No Results” Search Page Layout
As an experienced Magento developer, I often encounter requests to customize the layout of the search results page, especially for cases where no matching products are found. The default Magento setup uses a fixed layout regardless of whether a search yields zero or many results. Let me guide you through creating a custom template that adapts the layout based on the number of matching products.
Understanding Magento’s Search Process
Before diving into the code, it’s essential to understand how Magento handles searches and renders the results page. The catalogsearch/result controller is responsible for this process. When performing a search, Magento executes the following steps:
- It calls the
searchCollectionmethod of theMage_CatalogSearch_Model_Resource_Searchclass. - This method queries the database for matching products based on the entered keyword.
- The query result is then passed to the relevant template file (usually
catalogsearch/result.phtml) for rendering.
Defining a Custom Search Results Template
The key to customizing the layout lies in creating a new, custom template that extends the default result.phtml file. Follow these steps:
-
Navigate to your Magento theme directory:
/app/design/frontend/<package>/<theme>. -
Create a new directory named
catalogsearch(if it doesn’t exist already). -
Inside the
catalogsearchfolder, create a new file calledresult.phtml. -
Open the default
result.phtmlfile located in one of Magento’s core directories (usually/app/design/frontend/base/default/template/catalogsearch/) and copy its content. -
Paste this content into your custom
result.phtml, but make sure to add a layout condition at the beginning:
<?php if ($this->getProductCollection()->count() > 0): ?>
<!-- Your 2 column layout code here -->
<?php else: ?>
<!-- Your 1 column layout code here -->
<?php endif; ?>
Notice that I’ve used <?php if ($this->getProductCollection()->count() > 0): ?> to check whether any matching products exist in the search results.
Implementing the Layout Changes
Now, let’s create the actual layout changes within the inner PHP tags. For simplicity, I’ll use a basic one-column and two-column layout structure using Magento’s layout XML syntax.
- For the 1 column layout (when no results are found):
<div class="no-results-message">
<p>Your search for "<strong><?= $this->escapeHtml($this->getQueryText()) ?></strong>" did not yield any products.</p>
</div>
- For the 2 column layout (when at least one result is found):
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="<?= $this->getViewFileUrl('catalog/layout/update.xsd') ?>">
<reference name="root">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>
</layout>
In this 2 column layout, I’ve used a two-column right layout (page/2columns-right.phtml) as an example. You can replace it with your preferred layout XML file or create a new one if needed.
Conclusion
Customizing the “no results” search page in Magento allows you to provide a better user experience by adapting the layout based on the availability of matching products. By creating a custom result.phtml template and implementing the appropriate layout changes, you can achieve the desired 1 column or 2 column structure.
Remember that themes are highly customizable, so feel free to extend this example with your own design elements and styling as needed. If you’re looking for in-depth tutorials on Magento development, I recommend exploring our comprehensive guides at Laravel Company.
Happy coding!