簡體   English   中英

Magento - 如何將分層導航添加到高級搜索?

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

如何將“分層導航”添加到“高級搜索”結果頁面?

Magento版本1.7。

下面的補丁將在高級搜索結果中顯示分層導航,並且可以通過分層導航正常工作。 分層導航和搜索結果基於兩個單獨的產品集合顯示,一個由catalogsearch / Model / Layer.php創建,另一個由catalogsearch / Model / Advanced.php創建 因此,我們需要覆蓋這兩個模型的幾個函數,以使分層導航工作在高級搜索中。

1-在catalogsearch_advanced_result標記下的local.xml中添加。

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

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

對此沒有快速解決方案。 標准搜索和高級搜索使用兩種不同的方法進行搜索。

如果比較catalogsearch.xml的布局,則會看到對於catalogsearch_advanced_result ,不包括塊catalogsearch/layer 如果從catalogsearch_result_index復制塊定義並將根模板更改為3columns.phtml則會引發各種錯誤。

在我的1.6.2中,分層導航在設置0(零)后顯示出來
系統 - >配置 - >目錄 - >目錄搜索 - >如果搜索結果小於,則應用分層導航

這個鏈接到Magento網站應該有所幫助。 您需要從目錄創建屬性。 然后查看前端屬性(目錄>屬性)下的設置。

只需在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>

希望它能幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM