Magento 1.9.2 Dodaj nawigację po warstwach, aby przyspieszyć wyszukiwanie


12

Wykonałem 3 kroki dla zaawansowanej nawigacji w warstwie wyszukiwania, ale to nie działa. Wszelkie pomysły / sugestie LUB Jak zaimplementować nawigację po warstwach w wyszukiwaniu zaawansowanym?

1) W naszym pliku local.xml w tagu catalogsearch_advanced_result dodaj.

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

2) Zastąp funkcję preparProductCollection w katalogusearch / model / Layer.php za pomocą

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) Zastąp funkcję getProductCollection, getSearchCriterias w katalogusearch / model / Advanced.php za pomocą

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

Odpowiedzi:


1

Utwórz nowy plik w wersji lokalnej lub zastąp model warstwy w lokalnej aplikacji modułu / code / local / Mage / CatalogSearch / Model / Layer.php i zmień funkcję preparProductCollection

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

Po tym zrobić to samo z app / code / local / Mage / CatalogSearch / Model / Advanced.php i zmiana getProductCollection i getSearchCriterias funkcji

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

Tutaj poniżej nawigacja warstw jest przygotowana do zaawansowanego wyszukiwania produktów, teraz musi wyświetlić tę nawigację warstw przy użyciu pliku xml motywu, więc edytuj plik c atalogsearch.xml swojego motywu i dodaj poniższy kod w etykietka.

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

Aby uzyskać więcej informacji, odwiedź ten link


Działa jak urok!
Nalin Savaliya


0

Aby dodać nawigację warstw w celu wyszukiwania zaawansowanego, wykonaj następujące czynności: 1- W poniższej nawigacji przygotowana jest nawigacja w warstwie dla kolekcji wyszukiwania zaawansowanego. Teraz produkt musi wyświetlać nawigację warstw za pomocą pliku motywu xml, więc edytuj plik themesearchsearch.xml i dodaj poniższy kod pod tagiem.

    <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;
}
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.