首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重写Magento的AutoSuggest (Minisearch)

重写Magento的AutoSuggest (Minisearch)
EN

Stack Overflow用户
提问于 2013-08-16 07:05:32
回答 2查看 998关注 0票数 1

几个小时以来,我一直试图成功地重写Magento的内置自动建议函数,这样它就可以显示产品名称而不是查询历史条目。我不想要什么花哨的东西,没有产品图片什么的,只是简单的产品名称建议。

因此,为了获得产品名称,我在app/code/local/Aw下创建了文件夹CatalogSearch/Model,并在那里创建了一个名为Query.php的文件。在该文件中,我有以下类和重写的方法:

代码语言:javascript
复制
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;
    }
};

我在app/etc/ module /中创建了模块xml文件,在app/code/local/Aw/CatalogSearch/etc/config.xml中创建了模块配置

到目前为止,已经执行了覆盖的方法getSuggestCollection()

问题出现在app/code/core/Mage/CatalogSearch/Block/Autocomplete.php中,在getSuggestData()方法中。

代码语言:javascript
复制
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;
}

当它在集合上迭代时,我得到一个

代码语言:javascript
复制
Call to a member function getQueryText() on a non-object ...

我不明白的一点是,我在query_text方法中的集合查询中定义了一个别名字段‘getSuggestCollection()’。即使我使用像getData('query_text')$item->getQuery_text()这样的工具来获取这个字段的数据,也是行不通的。我有一种强烈的感觉,即集合对象在Mage_CatalogSearch_Block_Autocomplete类的getSuggestData()方法中是无效的。

有人能告诉我如何解决这个问题吗?难道不可能像上面那样从产品收集中收集建议并将它们传递给Autocomplete.php吗?

这是我的第一个magento项目,所以请容忍我!我真的迷上这个了!

任何暗示都被大大地引用了。

在此项目中使用Magento 1.7.0.2。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-17 04:10:52

我找到了解决办法。对于任何可能对此感兴趣的人,我问题中所述的问题如下所示

代码语言:javascript
复制
$collection = Mage::getModel('catalog/product');
Mage::getSingleton('catalog/product_status')
    ->addVisibleFilterToCollection($collection);
$collection->getCollection() ... // continue method chaining ...

我更改了代码,以便将构造函数和方法链接在一起,如下所示:

代码语言:javascript
复制
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name') ... // continue method chaining
...

在集合可用之后,我为product_statuscataloginventory/stockcatalog/product_visibility添加了单例调用的筛选器。

这样,一切都如预期的那样运作。

票数 1
EN

Stack Overflow用户

发布于 2014-04-14 13:26:47

对于任何想做类似事情的人,我只需将app/code/core/Mage/CatalogSearch/Block/Autocomplete.php重写到自己的模块中,并让搜索结果查询sku并返回产品名称。您的里程可能有所不同,然而,我的sku代码是明智的名称,而不是随机数字,所以这对我有效。

代码语言:javascript
复制
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;
}

我不需要重写Mage_CatalogSearch_Model_Query,只需要为建议编写代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18267656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档