Saturday, May 7, 2016

Add Layered Navigation to Advance Search Result Page in Magento


Magento Layered Navigation is used for search products according to categorize, price or different attribute which are assigned from Magento admin panel. So when users want to product by color, size than they can using Layered Navigation.
In Magento Layered Navigation display in left side of each category listing page if there are some products in category. It's also display in the left side column in search results page. Layered Navigation is auto generated according to a listing of products. if all products on listings attribute price and category are same then it's not displayed.
You can call layer navigation block on category listing page using catalog.xml file with below code.

<reference name="left">
  <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>
</reference>

That's define according to anchor and non anchor category in Magento.But it's not display in advance search results.so here describe simple way for display Layered Navigation in advance search result page by modifying core Magento file or you can rewrite that using custom module.
Change prepareProductCollection function as below in app/code/core/Mage/CatalogSearch/Model/Layer.php file.

public function prepareProductCollection($collection)
{
    if(Mage::helper('catalogsearch')->getQuery()->getQueryText())
    {
        $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
        ->setStore(Mage::app()->getStore())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addStoreFilter()
        ->addUrlRewrite();
    }
    else
    {
        $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());
        $attributes = Mage::getSingleton('catalog/product')->getAttributes();
        foreach($attributes as $attribute)
        {
            $attribute_code = $attribute->getAttributeCode();
            if($attribute_code == "price")
            continue;
            if (empty($_REQUEST[$attribute_code])){continue;}
            if(!empty($_REQUEST[$attribute_code]) && is_array($_REQUEST[$attribute_code]))
                $collection->addAttributeToFilter($attribute_code, array('in' => $_REQUEST[$attribute_code]));
            else
            if(!empty($_REQUEST[$attribute_code]))
            $collection->addAttributeToFilter($attribute_code, array('like' => "%" . $_REQUEST[$attribute_code] . "%"));
        }
        $collection->setStore(Mage::app()->getStore())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addStoreFilter()
        ->addUrlRewrite();
        Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection($collection);    
    }
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
    return $this;
}

Than after change getProductCollection and getSearchCriterias function as below in app/code/core/Mage/CatalogSearch/Model/Advanced.php file.

public function getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addStoreFilter();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
        if(isset($_GET['cat']) && is_numeric($_GET['cat']))
        $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['cat']),true);
    }
    return $this->_productCollection;
}
public function getSearchCriterias()
{
    $search = $this->_searchCriterias;
    if(isset($_GET['cat']) && is_numeric($_GET['cat'])){
        $category = Mage::getModel('catalog/category')->load($_GET['cat']);
        $search[] = array('name'=>'Category','value'=>$category->getName());
    }
    return $search;
}

Here in below layer navigation is prepared for advance search product collection now must display that layer navigation using theme xml file so edit your theme's catalogsearch.xml file and add below code in <catalogsearch_advanced_result> tag.
 
<reference name="left">
  <block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
 
here add that navigation in left sidebar you can put in content or right side according to theme content or template.

6 comments :

  1. I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies. Could you please extend them a little from next time?..Keep this great work
    magento development company in bangalore 

    ReplyDelete
  2. Got Error as Fatal error: Call to a member function getSortedChildren() on a non-object in

    ReplyDelete
  3. hello i use this code but nothing is working. plz any one help me.

    ReplyDelete
    Replies
    1. Check any other extesion related to search you install or any rewrite code use.

      Delete
  4. great post, how to do this in magento 2 ?

    ReplyDelete