8

如何将分层导航添加到高级搜索结果页面?

Magento 1.7 版。

4

5 回答 5

8

下面的补丁将在高级搜索结果中显示分层导航,并且可以与分层导航一起正常工作。分层导航和搜索结果基于两个单独的产品集合显示,一个由catalogsearch/Model/Layer.php创建,另一个由catalogsearch/Model/Advanced.php创建。所以我们需要重写这两个模型的一些功能,以使分层导航在高级搜索中工作。

1- 在您的 local.xml 中catalogsearch_advanced_result标签下添加。

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

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

覆盖catalogsearch/model/Advanced.php的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;
}
于 2014-09-02T02:17:45.017 回答
5

对此没有快速的解决方案。标准搜索和高级搜索使用两种不同的搜索方法。

如果您比较其中的布局,catalogsearch.xml您会发现catalogsearch_advanced_result该块catalogsearch/layer不包括在内。如果您从catalogsearch_result_index根模板复制块定义并将其更改为3columns.phtml各种错误,则会引发各种错误。

于 2012-11-13T11:41:41.813 回答
1

在我的 1.6.2 中,分层导航在将 0(零)设置为
系统 -> 配置 -> 目录 -> 目录搜索 -> 如果搜索结果小于时应用分层导航后出现

于 2013-02-18T09:23:45.890 回答
0

catalogsearch.xml只需在搜索结果左侧区域提前添加以下行就可以帮助我在我的 EE 网站上看到它,但是我没有在 CE 版本中检查它。

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

所以我的整个左侧区域在 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>

希望它可以帮助别人。

于 2014-08-06T00:52:42.063 回答
0

这个链接到 Magento 网站应该会有所帮助。您需要从目录创建属性。然后查看前端属性下的设置(目录>属性)。

于 2012-08-23T01:03:02.910 回答