几个小时以来,我一直试图成功地重写Magento的内置自动建议函数,这样它就可以显示产品名称而不是查询历史条目。我不想要什么花哨的东西,没有产品图片什么的,只是简单的产品名称建议。
因此,为了获得产品名称,我在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/ module /中创建了模块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 ...我不明白的一点是,我在query_text方法中的集合查询中定义了一个别名字段‘getSuggestCollection()’。即使我使用像getData('query_text')或$item->getQuery_text()这样的工具来获取这个字段的数据,也是行不通的。我有一种强烈的感觉,即集合对象在Mage_CatalogSearch_Block_Autocomplete类的getSuggestData()方法中是无效的。
有人能告诉我如何解决这个问题吗?难道不可能像上面那样从产品收集中收集建议并将它们传递给Autocomplete.php吗?
这是我的第一个magento项目,所以请容忍我!我真的迷上这个了!
任何暗示都被大大地引用了。
在此项目中使用Magento 1.7.0.2。
发布于 2013-08-17 04:10:52
我找到了解决办法。对于任何可能对此感兴趣的人,我问题中所述的问题如下所示
$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
...在集合可用之后,我为product_status、cataloginventory/stock和catalog/product_visibility添加了单例调用的筛选器。
这样,一切都如预期的那样运作。
发布于 2014-04-14 13:26:47
对于任何想做类似事情的人,我只需将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_Query,只需要为建议编写代码。
https://stackoverflow.com/questions/18267656
复制相似问题