2010-08-26 109 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>'; 
}