Does anyone have any idea where I would start regarding having CMS pages show up in search results, not just products on the front end? I know you can do a redirect with a keyword, but I don't want to do that. I want the CMS page to actually show up in the search results.
CMS – How to Add Other Pages to Search Results Besides Just Products
catalogsearchcmssearch
Related Solutions
The key to the fact that the layer block needs to be there can be found in Mage_CatalogSearch_Block_Layer
in the construct.
/**
* Internal constructor
*/
protected function _construct()
{
parent::_construct();
Mage::register('current_layer', $this->getLayer(), true);
}
The second line is the key: Mage::register('current_layer', $this->getLayer(), true);
without this line none of the search results will display correctly. What this does is tell the system what the current state of the search is.
This will set the current layer as type Mage_CatalogSearch_Model_Layer
but if this line is not run then later on the layer gets set as type Mage_Catalog_Model_Layer
. The two models have two similar but different prepareCollection functions.
Mage_CatalogSearch_Model_Layer
Has an important call to addSearchFilter
public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
->setStore(Mage::app()->getStore())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addStoreFilter()
->addUrlRewrite();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
return $this;
}
Mage_Catalog_Model_Layer
Has no addition of addSearchFilter and does not set or filter on the store.
public function prepareProductCollection($collection)
{
$collection
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addUrlRewrite($this->getCurrentCategory()->getId());
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
return $this;
}
It may depends of the number of results.
Open the search settings at admin - system - configurations - catalog - catalog search and find the option "Apply Layered Navigation if Search Results are Less Than"
Enter "0" to enable layered navigation for any number of results.
Best Answer
I think there are two ways to achieve this: changing CatalogSearch and adding a new block.
Changing CatalogSearch
You can change the Result Block, adding a cms search to the query, union it or just add the results to the result array afterwards. However you do it, you have to take care of the difference between cms page and product results.
To change the catalogSearch I think the best starting point is the
Mage_CatalogSearch_Model_Resource_Fulltext_Collection
. The big problem is differ between a product and a cms page. Maybe it is a good idea to extend theMage_Catalog_Model_Product
class, use it in the collection to initialize new collection items and have a method to distinguish between products and cms pages.Then you can just check in the block for
$item->getType()
or something and handle the output accordingly.This are just a few ideas, I have no idea wether it works this way. Good luck in trying and please report back!
Adding a second block
You can implement a second block under/over the product search results which searches in the cms pages. Depending on the size of cms pages an index might be a good idea but normal it is not needed, I think.
If you need further informations, tell me, then I'll write more with a better device than an ipad and source code by hand.