Customizing Magento CatalogSearch Result HTML Layout
As an e-commerce developer, you may find yourself needing to customize the layout of your Magento store’s catalog search result page. This can be particularly useful when you have only a few products or want to streamline the user experience by removing unnecessary elements.
Understanding the Default Layout
The default Magento CatalogSearch result layout includes various elements such as pagination, sort options, and product filters. To locate these elements in the codebase, you’ll notice that the catalogsearch/result template calls $this->getProductListHtml().
This function is responsible for rendering the list of search results. The default implementation uses the core Magento templates (catalog/product/list.phtml) to display the products with the standard layout.
Customizing the Product List
To start customizing your search result page, you’ll need to modify how the product list is rendered. This involves overriding the getProductListHtml() function in your theme or extension.
-
Create a new template file
Navigate to your current Magento theme directory (e.g.,
app/design/frontend/<Vendor>/<Theme>/Magento_Catalog/templates/product/list.phtml). Create a copy of this file and name it something likecatalogsearch_result_list.phtml. -
Modify the product list HTML
Open your new template file and start modifying the HTML structure to match your desired layout. You can remove elements such as pagination, sort options, and filters by commenting out or deleting the corresponding code blocks.
-
Customize product display
To change how products are displayed, you’ll need to modify the
<div class="product-item-info">section of each product loop. For example, you can wrap your product images in a custom div with a rollover effect:
<div class="image-container">
<a href="<?php /* @escapeNotVerified */ echo $block->getProductUrl($item) ?>">
<?php /* @escapeNotVerified */ echo $block->getImageHTML($item, 'category_page_list') ?>
</a>
</div>
In this example, we’ve created a new image-container div around the product image. You can add CSS styling to this class in your theme’s css directory.
-
Set the template path
To apply your custom template file, you’ll need to set the template path for the search result page. This is done using the layout XML.
Open your active theme’s
app/design/frontend/<Vendor>/<Theme>/layout/catalogsearch.xmlfile (create it if it doesn’t exist). Add or modify the following section:
<referenceContainer name="content">
<block class="Magento\CatalogSearch\Block\Result" template="Magento_Catalog::product/list.phtml"/>
</referenceContainer>
Change Magento_Catalog::product/list.phtml to point to your custom file, e.g., <template>catalogsearch_result_list.phtml</template>.
Conclusion
Customizing the Magento CatalogSearch result HTML layout involves a combination of modifying template files and updating layout XML. By following this process, you can create a streamlined, customized search experience for your customers.
Remember to always keep your code organized by placing customizations in extension or theme files separate from the core Magento codebase. This ensures that your changes are preserved during updates and can be easily maintained.
For more advanced customization techniques and best practices, consider exploring the official Magento documentation on Layout XML Reference and Theme Development at https://laravelcompany.com.
Happy coding!