When I try to do search by Category Name it reurns nothing. For Eg, I have Organic, Unique, Sprots etc.as categories and in search I type Unique. But I get no results.
You can search categories using LIKE filter as below
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('url')
->addAttributeToSelect('name')
->addAttributeToFilter('name',array(array('like' => '%'. $searchvariable.'%')));
Results Output
foreach ($categories as $cat) {
echo '<div><a href="'.$cat->getUrl().'">' . $cat->getName() . '</a></div>';
}
You may be looking for the addAttributeToFilter method. e.g.
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToFilter('name',$name);
You can then work with the collection returned, e.g.
foreach ($categories as $cat) {
echo 'Name: ' . $cat->getName() . "<br />";
echo 'Category ID: ' . $cat->getId() . "<br />";
}
This works in Magento CE 1.7.0.1, at least.
Unfortunately, Magento's default search function is a product search and is limited to that scope. When you search "Unique" it's looking in the products name and perhaps the description depending on your configuration.
A quick solution would be to display a listing of matching categories along with the product results.
<?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;?>
Source: http://www.magentocommerce.com/boards/viewthread/74632/