2010-08-26 229 views
3

當我嘗試按類別名稱進行搜索時,它什麼也不會返回。例如,我有有機,獨特,Sprots etc.asas類別和搜索我輸入唯一。但我沒有結果。按分類名稱搜索

+0

請發表您用來執行這些搜索,並修改您的文章,包括問題的代碼。 – 2010-08-26 23:53:00

回答

6

不幸的是,Magento的默認搜索功能是產品搜索,並且僅限於該範圍。當您搜索「唯一」時,它會查看產品名稱和描述,具體取決於您的配置。

快速解決方案是顯示匹配類別列表以及產品結果。

<?php 
    $searchTerm = $this->helper('catalogSearch')->getEscapedQueryText(); 
    $categories = $this->helper('catalog/category')->getStoreCategories(false, true); 
    $count = 0; 
    foreach ($categories as $count_category) { 
     if ($this->helper('catalog/category')->canShow($count_category) && stripos($count_category->getName(), $searchTerm) !== false) 
       $count++; 
    } 

    if ($count > 0): 

    echo "<div class=\"search-term-notice\">"; 
    echo "The following product categories matched your search:"; 

    foreach ($categories as $category) { 
     if ($this->helper('catalog/category')->canShow($category) && stripos($category->getName(), $searchTerm) !== false) 
      echo "<h3> > <a href='".$category->getUrl()."'>".$category->getName()."</a></h3></p>"; 
    } 
    echo "</div>"; 
    endif;?> 

來源:http://www.magentocommerce.com/boards/viewthread/74632/

-1

你可能會尋找addAttributeToFilter方法。例如

$categories = Mage::getModel('catalog/category')->getCollection() 
->addAttributeToSelect('id') 
->addAttributeToSelect('name') 
->addAttributeToFilter('name',$name); 

然後,您可以處理返回的集合,例如

foreach ($categories as $cat) { 
    echo 'Name: ' . $cat->getName() . "<br />"; 
    echo 'Category ID: ' . $cat->getId() . "<br />"; 
} 

這在Magento CE 1.7.0.1中起作用,至少。

0

您可以搜索使用類似過濾器的類別如下

$categories = Mage::getModel('catalog/category')->getCollection() 
    ->addAttributeToSelect('url') 
    ->addAttributeToSelect('name') 
    ->addAttributeToFilter('name',array(array('like' => '%'. $searchvariable.'%'))); 

結果輸出

foreach ($categories as $cat) { 
    echo '<div><a href="'.$cat->getUrl().'">' . $cat->getName() . '</a></div>'; 
}