質問

I been trying for hours now to successfully rewrite Magento's build-in Autosuggest Function so it displays productnames instead of query history entries. I want nothing fancy, no product pictures and whatnot, just plain product name suggestions.

So to get the productnames, I created under app/code/local/Aw the folder CatalogSearch/Model and there created a file named Query.php. Inside that file I have the following class and 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 the module xml file in app/etc/modules/ and the module configuration in app/code/local/Aw/CatalogSearch/etc/config.xml

All good so far, the overwritten method getSuggestCollection() is executed.

The problem comes in app/code/core/Mage/CatalogSearch/Block/Autocomplete.php, in the getSuggestData() method.

public function getSuggestData()
{
    if (!$this->_suggestData) {
        $collection = $this->helper('catalogsearch')->getSuggestCollection();
        $query = $this->helper('catalogsearch')->getQueryText();
        $counter = 0;
        $data = array();
        foreach ($collection as $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 it iterates over the collection, I get a

Call to a member function getQueryText() on a non-object ...

The point I do not understand is that I have defined an alias field named 'query_text' in the collection query inside the getSuggestCollection() method. Even when I used something like getData('query_text') or $item->getQuery_text() to get the data of this field is not working. I have the strong feeling, that the collection object is not valid as it supposed be within the getSuggestData() method of Mage_CatalogSearch_Block_Autocomplete class.

Can anybody point me out how to solve this issue? Is it not possible as above way to gather suggestions from the products collection and pass these to Autocomplete.php?

This is my first magento project, so please bear with me! I am really lost on this one!

Any hint is greatly apprecitated.

Using Magento 1.7.0.2 for this project.

役に立ちましたか?

解決

Well, I found a solution. For anyone who might be interested in this, the problem stated in my question is located in the following lines

$collection = Mage::getModel('catalog/product');
Mage::getSingleton('catalog/product_status')
    ->addVisibleFilterToCollection($collection);
$collection->getCollection() ... // continue method chaining ...

I changed the code, so that the constructor and methods are chained all together, like this:

$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name') ... // continue method chaining
...

I added the filters for product_status, cataloginventory/stock and catalog/product_visibility with singleton calls right after the collection is available
In that way, everything works as expected.

他のヒント

For anyone else wanting to do something similar, I just rewrote app/code/core/Mage/CatalogSearch/Block/Autocomplete.php to my own module and made the search results query the sku and return product names. Your mileage may vary, however, my sku codes are sensible names rather than random digits so this worked for me.

public function getSuggestData()
{
    if (!$this->_suggestData) {
        $collection = $this->helper('catalogsearch')->getSuggestCollection();
        $query = $this->helper('catalogsearch')->getQueryText();
        $counter = 0;
        $data = array();
        foreach ($collection as $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;
            }
        }


        // Get products where the url matches the query in some meaningful way

        $products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToFilter('type_id', 'configurable')
            ->addAttributeToFilter('sku',array('like'=>'%'.$query.'%'))
            ->load();
        foreach($products as $product) {
            $_data = array(
                'title' => $product->getName(),
                'row_class' => (++$counter)%2?'odd':'even',
                'num_of_results' => 1
            );

//                if ($item->Name() == $query) {
//                    array_unshift($data, $_data);
//                }
//                else {
                $data[] = $_data;
//                }
        }

        $this->_suggestData = $data;
    }
    return $this->_suggestData;
}

I did not need to rewrite Mage_CatalogSearch_Model_Query, just the code for the suggestions.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top