12

I have followed following 3 steps for advanced search layer navigation, but it is not working. Any idea/suggestion OR How to implement layer navigation in advanced search?

1) In our local.xml under catalogsearch_advanced_result tag add.

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

2) Override prepareProductCollection function of catalogsearch/model/Layer.php with

public function prepareProductCollection($collection){

    if(Mage::helper('catalogsearch')->getQuery()->getQueryText())//for normal search we get the value from query string q=searchtext
        return parent::prepareProductCollection($collection);
    else{

        $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());
        /**
         * make sure you cross check the $_REQUEST with $attributes
         */
        $attributes = Mage::getSingleton('catalog/product')->getAttributes();

        Mage::log(print_r($_REQUEST,1));
        foreach($attributes as $attribute){
            $attribute_code = $attribute->getAttributeCode();
            //Mage::log("--->>". $attribute_code);
            if($attribute_code == "price")//since i am not using price attribute
                continue;

            if (empty($_REQUEST[$attribute_code])){
                //Mage::log("nothing found--> $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::log($collection->getSelect()->__toString());

        Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection($collection);    
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
    }

    return $this;
}

3) Override getProductCollection, getSearchCriterias function of catalogsearch/model/Advanced.php with

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 = parent::getSearchCriterias();
    /* display category filtering criteria */
    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;
}

3 Answers 3

1

Create new file in local or override Layer model in local module app/code/local/Mage/CatalogSearch/Model/Layer.php file and Change prepareProductCollection function

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

After that do same with app/code/local/Mage/CatalogSearch/Model/Advanced.php and change getProductCollection and getSearchCriterias functions

    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 tag.

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

For More information visit this link

1
  • It's working like charm! Commented Jul 6, 2017 at 12:16
0

To show the layered navigation in advanced search you have to make some changes in core file.

Follow this link I have use the same process and it is working fine.

https://newsinfo-blog.blogspot.in/2016/05/add-layered-navigation-to-advance.html

2
  • Consider adding the information from that link to your post, link's are getting expired, page's closed.
    – versedi
    Commented Feb 3, 2017 at 9:31
  • Just looking for visitors, it sucks :/ Commented Mar 6, 2017 at 16:18
0

For add layer navigation to advance search follow the below steps: 1- 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 catalogsearch.xml file and add below code under tag.

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

2. Override prepareProductCollection function of catalogsearch/model/Layer.php with this

    public function prepareProductCollection($collection)
    {   
        $bac= Mage::helper('catalogsearch')->getQuery()->getQueryText();
        if($bac)
        {
            $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;
    }

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.