重写Magento的自动建议(小型搜索)
我已经尝试了几个小时来成功重写Magento的内置Autosuggest功能,以便它显示产品名称而不是查询历史记录条目。我不要花哨的东西,不要产品图片什么的,只要简单的产品名称建议 为了获得产品名称,我在重写Magento的自动建议(小型搜索),magento,autosuggest,overwrite,Magento,Autosuggest,Overwrite,我已经尝试了几个小时来成功重写Magento的内置Autosuggest功能,以便它显示产品名称而不是查询历史记录条目。我不要花哨的东西,不要产品图片什么的,只要简单的产品名称建议 为了获得产品名称,我在app/code/local/Aw文件夹CatalogSearch/Model下创建了一个名为Query.php的文件。在该文件中,我有以下类和重写方法: class Aw_CatalogSearch_Model_Query extends Mage_CatalogSearch_Mod
app/code/local/Aw
文件夹CatalogSearch/Model
下创建了一个名为Query.php
的文件。在该文件中,我有以下类和重写方法:
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/modules/中创建了模块xml文件,在app/code/local/Aw/CatalogSearch/etc/config.xml中创建了模块配置
到目前为止一切正常,执行覆盖的方法getSuggestCollection()
问题出现在app/code/core/Mage/CatalogSearch/Block/Autocomplete.php中的getSuggestData()
方法中
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;
}
当它在集合上迭代时,我得到一个
Call to a member function getQueryText() on a non-object ...
我不明白的是,我在getSuggestCollection()
方法内的集合查询中定义了一个名为“query\u text”的别名字段。即使我使用类似于getData('query\u text')
或$item->getQuery\u text()
的方法来获取此字段的数据,也无法工作。
我有一种强烈的感觉,集合对象在Mage\u CatalogSearch\u Block\u Autocomplete
类的getSuggestData()方法中是无效的
有人能告诉我如何解决这个问题吗?难道不能像上面那样从产品集合中收集建议并将其传递给Autocomplete.php吗
这是我的第一个magento项目,请耐心等待!我真的迷路了
任何暗示都是非常明智的
在这个项目中使用Magento 1.7.0.2。我找到了一个解决方案。对于任何可能对此感兴趣的人,我的问题中所述的问题位于以下几行
$collection = Mage::getModel('catalog/product');
Mage::getSingleton('catalog/product_status')
->addVisibleFilterToCollection($collection);
$collection->getCollection() ... // continue method chaining ...
我更改了代码,以便将构造函数和方法链接在一起,如下所示:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name') ... // continue method chaining
...
我添加了产品\ u状态
、目录库存/库存
和目录/产品\ u可见性
的过滤器,这些过滤器在集合可用后立即使用单例调用
通过这种方式,一切都按预期进行。对于其他想要做类似事情的人,我只需将app/code/core/Mage/CatalogSearch/Block/Autocomplete.php重写到我自己的模块中,让搜索结果查询sku并返回产品名称。您的里程数可能会有所不同,但是,我的sku代码是合理的名称,而不是随机数字,所以这对我很有用
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_查询,只需要为建议编写代码