When we search any text in default magento it shows only products in the search result but I want to show the categories also when someone searches for a query string.
Like if query string is like catalogsearch/result/?q=brother
Then the result should contain category and products.
The result should also be formatted like first we show the category then it's products that match the query string then second category and it's products and so on. If category match not found show only products and if product match is not found then show categories which match the query string.
What I want is the list view which contains both categories and products just like http://www.ldproducts.com/ldpsearch/result/?q=officejet+pro+8000
Goto this URL and scroll till the bottom and you'll see what I exactly want.
Googled a lot but was unable to find the desired answer. I am not supposed to use any extension or make any changes on the phtml file(until it's damn mandatory).
My magento version is 1.9.0.1
Best Answer
Interesting question, hence upvoting it. Below is the code for achieving the categories and the corresponding products as results. I have planned to reuse the FILTERS (Layered Navigation) concept to be able to display the categories and products as per the screenshots here:
These screenshots are currently not displaying the correct price, name etc. Hence this might need to be checked in the product collection that is present in the code written below. Hope you can figure out how to bring that and display it.
Step 1:
Create the below mentioned files.
See each file content as below:
1. /app/code/local/Namespace/Modulename/Block/Catalog/Product/List.php
2. /app/code/local/Namespace/Modulename/Block/CatalogSearch/Layer/Filter/Category.php
3. /app/code/local/Namespace/Modulename/Block/CatalogSearch/Layer/State.php
4. /app/code/local/Namespace/Modulename/Block/CatalogSearch/Layer.php
5. /app/code/local/Namespace/Modulename/etc/config.xml
6. /app/etc/modules/Namespace_Modulename.xml
I am sure you can create this :-)
7. /app/design/frontend/rwd/default/layout/catalogsearch.xml
/app/design/frontend/rwd/default/template/catalogsearch/product/list.phtml
/app/design/frontend/rwd/default/template/catalogsearch/layer/filter.phtml
/app/design/frontend/rwd/default/template/catalogsearch/layer/state.phtml
This file MAY NOT be needed. I have kept it to just make sure the reusing of filters works correctly or not.
/app/design/frontend/rwd/default/template/catalogsearch/layer/view.phtml
DISCLAIMER: I was able to work on this only to produce relevant results how the question was described above. To be able to display categories and products based on the search term. I am not sure if this will work to fetch only categories or only products. This needs to be checked based on our discussion in the chat.
As a note: currently the left navigation on the search page is not working as expected based on the code I have done. That also might need to be checked.
Hope this helps you and someone.
Happy Coding...