Displaying Magento Search Results on Custom Pages
In Magento, you can restrict the default search result redirection and display the retrieved products on your custom pages using XML layout updates and PHP code. Here’s a step-by-step guide to achieve this.
Understanding the Default Magento Search Functionality
By default, when users enter a search query in Magento, they are redirected to the catalogsearch/result/?q="some text" page where the matching products are displayed. This redirection is handled by the core Magento search functionality.
The search results are generated using the Mage_CatalogSearch_Model_Resource_Fulltext model and the fulltext table in your database which stores the full-text index of your product catalog.
Creating a Custom Search Results Page
To display the search results on a custom page like index.php, you’ll need to create a new XML layout update file and PHP template file.
Step 1: Create an XML Layout Update File
Open or create the app/design/frontend/your_package/your_theme/layout/local.xml file (create these directories if they don’t exist). Add the following code:
<layout version="0.1.0">
<default>
<reference name="content">
<block type="catalogsearch/result" template="catalogsearch/result.phtml"/>
</reference>
</default>
</layout>
This layout update tells Magento to include the catalogsearch/result block in your custom page’s content area.
Step 2: Create a Custom PHP Template File
Create or open the app/design/frontend/your_package/your_theme/template/catalogsearch/result.phtml file. This is where you’ll handle the display of search results using PHP code.
Here’s an example of how to retrieve and display the search results:
<?php
// Get the query parameter from the URL
$query = $this->getRequest()->getQuery('q');
// If a search query was provided
if ($query) {
// Use Magento's resource model to get the search results
$collection = Mage::getResourceModel('catalogsearch/fulltext_collection')
->setQuery($query)
->addStoreFilter(Mage::app()->getStore()->getId());
if (count($collection)) {
foreach ($collection as $product) {
echo '<p><a href="' . $this->getUrl('catalog/product/view', array('id' => $product->getId())) . '">' . $product->getName() . '</a></p>';
}
} else {
echo 'No results found for "' . $query . '"';
}
} else {
echo 'Please enter a search query.';
}
?>
This PHP code retrieves the search query from the URL, uses Magento’s full-text collection to get the matching products, and then loops through each product to display its name as a link.
Restricting Default Search Redirection
To prevent users from being automatically redirected to the default catalogsearch/result/ page, you can add the following code to your custom search form:
<form action="<?php echo $this->getUrl('index/index') ?>" method="GET">
<input type="text" name="q" placeholder="Search..."/>
<button type="submit">Search</button>
</form>
This form submits the search query to your custom index.php page instead of the default Magento search result page.
Conclusion
By following these steps, you can successfully display Magento search results on a custom page like index.php while restricting the default redirection. This provides a more personalized user experience and gives you greater control over how search results are presented in your store.
Remember to replace your_package and your_theme with your actual Magento theme details. You may also want to style the displayed search results using CSS for better presentation.
For more advanced customization options, consider exploring the full-text search capabilities of Laravel Company’s Elasticsearch Search module which provides improved performance and flexibility for Magento search functionality.