Magento - How to add Layered Navigation to Advanced Search?

偶尔善良 提交于 2019-11-29 19:12:23

问题


How can I add Layered Navigation to the Advanced Search result pages?

Magento Version 1.7.


回答1:


The patch below will display the layered navigation in Advanced search result and will work fine with layered navigations. The layered navigation and search result are displayed based on two separate product collections, one created by catalogsearch/Model/Layer.php and the other by catalogsearch/Model/Advanced.php. So we need to override few functions of both these models to make layered nav work in Advanced search.

1- In your 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>

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

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



回答2:


There is no quick solution for this. The standard search and the advanced search use two different methods to search.

If you compare the layouts in catalogsearch.xml you see that for catalogsearch_advanced_result the block catalogsearch/layer is not included. If you copy the block definition from catalogsearch_result_index and change the root template to 3columns.phtml various errors are thrown.




回答3:


In my 1.6.2 the layered nav showed up after setting a 0 (Zero) to
System -> Configuration -> Catalog -> Catalog Search -> Apply Layered Navigation if Search Results are Less Than




回答4:


This link goes to Magento website should help. You need to create attributes from Catalogues. Then see the settings under Frontend Properties (Catalogues>Attributes).




回答5:


Simply adding following line in catalogsearch.xml advance search results left area helped me to get it visible on my EE site, however I haven't checked it in CE version.

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

So my full left area looks like this on advance search area on xml file:

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

Hope it helps others.



来源:https://stackoverflow.com/questions/10503447/magento-how-to-add-layered-navigation-to-advanced-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!