Rewriting AutoSuggest (Minisearch) of Magento -
i been trying hours rewrite magento's build-in autosuggest function displays productnames instead of query history entries. want nothing fancy, no product pictures , whatnot, plain product name suggestions.
so productnames, created under app/code/local/aw folder catalogsearch/model , there created file named query.php. inside file have following class , rewritten method:
class aw_catalogsearch_model_query extends mage_catalogsearch_model_query { public function getsuggestcollection() { $collection = $this->getdata('suggest_collection'); if (is_null($collection)) { $collection = mage::getmodel('catalog/product'); mage::getsingleton('catalog/product_status') ->addvisiblefiltertocollection($collection); $collection->getcollection() ->addattributetoselect('name') ->addattributetofilter('name', array('like' => '%'.$this->getquerytext().'%')) ->addexpressionattributetoselect('query_text', '{{name}}', 'name') ->addattributetosort('name', 'asc') ->setpagesize(10) ->addstorefilter($this->getstoreid()); $this->setdata('suggest_collection', $collection); } return $collection; } }; i created module xml file in app/etc/modules/ , module configuration in app/code/local/aw/catalogsearch/etc/config.xml
all far, overwritten method getsuggestcollection() executed.
the problem comes in app/code/core/mage/catalogsearch/block/autocomplete.php, in getsuggestdata() method.
public function getsuggestdata() { if (!$this->_suggestdata) { $collection = $this->helper('catalogsearch')->getsuggestcollection(); $query = $this->helper('catalogsearch')->getquerytext(); $counter = 0; $data = array(); foreach ($collection $item) { $_data = array( 'title' => $item->getquerytext(), 'row_class' => (++$counter)%2?'odd':'even', 'num_of_results' => $item->getnumresults() ); if ($item->getquerytext() == $query) { array_unshift($data, $_data); } else { $data[] = $_data; } } $this->_suggestdata = $data; } return $this->_suggestdata; } when iterates on collection,
call member function getquerytext() on non-object ... the point not understand have defined alias field named 'query_text' in collection query inside getsuggestcollection() method. when used getdata('query_text') or $item->getquery_text() data of field not working. have strong feeling, collection object not valid supposed within getsuggestdata() method of mage_catalogsearch_block_autocomplete class.
can point me out how solve issue? not possible above way gather suggestions products collection , pass these autocomplete.php?
this first magento project, please bear me! lost on one!
any hint apprecitated.
using magento 1.7.0.2 project.
well, found solution. might interested in this, problem stated in question located in following lines
$collection = mage::getmodel('catalog/product'); mage::getsingleton('catalog/product_status') ->addvisiblefiltertocollection($collection); $collection->getcollection() ... // continue method chaining ... i changed code, constructor , methods chained together, this:
$collection = mage::getmodel('catalog/product') ->getcollection() ->addattributetoselect('name') ... // continue method chaining ... i added filters product_status, cataloginventory/stock , catalog/product_visibility singleton calls right after collection available
in way, works expected.
Comments
Post a Comment