当我尝试按类别名称进行搜索时,它什么都不会重复。对于Eg,我有Organic,Unique,Sprots等类别,在搜索中我输入Unique。但我没有结果。
答案 0 :(得分:7)
不幸的是,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;?>
答案 1 :(得分:0)
您可以使用LIKE过滤器搜索类别,如下所示
$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>';
}
答案 2 :(得分:-1)
您可能正在寻找addAttributeToFilter方法。 e.g。
$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中使用。