2013-08-16 29 views
1

我一直在嘗試幾個小時才能成功重寫Magento的內置Autosuggest Function,以便顯示產品名稱而不是查詢歷史記錄條目。我不想要任何花哨,沒有產品圖片和什麼,只是簡單的產品名稱的建議。重寫Magento的AutoSuggest(Minisearch)

爲了獲得產品名稱,我在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; 
    } 
}; 

我創建了應用程序的/ etc /模塊的模塊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_TEXT」的別名場。即使當我使用getData('query_text')$item->getQuery_text()之類的東西來獲取此字段的數據不起作用時。 我有強烈的感覺,收集對象是無效的,因爲它應該在Mage_CatalogSearch_Block_Autocomplete類的getSuggestData()方法內。

任何人都可以指出我如何解決這個問題?是否可以按照上述方式從產品收集中收集建議並將其傳遞給Autocomplete.php?

這是我第一個magento項目,請耐心等待!我真的迷失在這一個!

任何提示都非常重要。

在這個項目中使用Magento 1.7.0.2。

回答

1

嗯,我找到了一個解決方案。對於任何人誰可能有興趣在此,問題陳述我的問題是位於以下行

$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_statuscataloginventory/stockcatalog/product_visibility與單電話後立即收集可用
這樣一來,一切正常。

1

對於其他想要做類似事情的人,我只是重寫了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,只是建議的代碼。