Magento 1.9.2添加图层导航以进行高级搜索


12

我已经按照以下3个步骤进行了高级搜索层导航,但是它不起作用。任何想法/建议或如何在高级搜索中实现图层导航?

1)在我们的local.xml下的catalogsearch_advanced_result标记下添加。

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

2)使用覆盖重写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;
}

3)使用以下方法覆盖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;
}

Answers:


1

在本地创建新文件或在本地模块app / code / local / Mage / CatalogSearch / Model / Layer.php文件中覆盖Layer模型,并更改prepareProductCollection函数

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

之后,对app / code / local / Mage / CatalogSearch / Model / Advanced.php进行相同操作,并更改getProductCollectiongetSearchCriterias函数

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

在下面的层中,导航已为高级搜索产品准备就绪,现在必须使用主题xml文件显示该层导航,因此编辑主题的c atalogsearch.xml文件并在 标签。

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

有关更多信息,请访问此链接


就像魅力一样运作!
Nalin Savaliya'7


0

若要将图层导航添加到高级搜索,请执行以下步骤:1-在下面的图层导航中准备进行高级搜索,现在产品集合必须使用主题XML文件显示该图层导航,因此请编辑您的theme catalogsearch.xml文件并在标记下添加以下代码。

    <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;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.