简体   繁体   English

Magento - 如何将分层导航添加到高级搜索?

[英]Magento - How to add Layered Navigation to Advanced Search?

How can I add Layered Navigation to the Advanced Search result pages? 如何将“分层导航”添加到“高级搜索”结果页面?

Magento Version 1.7. Magento版本1.7。

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 . 分层导航和搜索结果基于两个单独的产品集合显示,一个由catalogsearch / Model / Layer.php创建,另一个由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. 1-在catalogsearch_advanced_result标记下的local.xml中添加。

 <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 用。覆盖catalogsearch / model / Layer.php的prepareProductCollection函数

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 覆盖getsearch产品的getProductCollection,getSearchCriterias函数

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

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. 如果比较catalogsearch.xml的布局,则会看到对于catalogsearch_advanced_result ,不包括块catalogsearch/layer If you copy the block definition from catalogsearch_result_index and change the root template to 3columns.phtml various errors are thrown. 如果从catalogsearch_result_index复制块定义并将根模板更改为3columns.phtml则会引发各种错误。

In my 1.6.2 the layered nav showed up after setting a 0 (Zero) to 在我的1.6.2中,分层导航在设置0(零)后显示出来
System -> Configuration -> Catalog -> Catalog Search -> Apply Layered Navigation if Search Results are Less Than 系统 - >配置 - >目录 - >目录搜索 - >如果搜索结果小于,则应用分层导航

This link goes to Magento website should help. 这个链接到Magento网站应该有所帮助。 You need to create attributes from Catalogues. 您需要从目录创建属性。 Then see the settings under Frontend Properties (Catalogues>Attributes). 然后查看前端属性(目录>属性)下的设置。

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. 只需在catalogsearch.xml添加以下行,预先搜索结果左侧区域帮助我在EE网站上显示它,但是我没有在CE版本中检查它。

<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: 所以我的左侧区域在xml文件的高级搜索区域看起来像这样:

<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. 希望它能帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM