Magento – Removing catalog/layer/view.phtml gives wrong search results

catalogsearchlayoutsearch

I'm having an issue with search results. I was switching my search results page over to a one column layout and removed the sidebar in catalogsearch.xml by taking out

    <reference name="left">
        <block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
    </reference>

When I removed that, the search results that were returned were incorrect, but when I leave it in, the search results are correct. The page successfully got switched over to a 1column layout, so overall, the problem is solved, however it would be nice to know why I need that in the xml to return proper results and it would be nice to remove unneeded code. I'm not seeing anything in that file that should effect the collection that gets returned.

Best Answer

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;
}