Solving Magento Category Search with LIKE Query
The Problem: Magento’s LIKE Query Not Working as Expected
You’ve written a solid piece of code to fetch categories using the LIKE query in Magento. Your aim is to auto-suggest categories based on user input. However, you’re facing an issue where the LIKE query isn’t functioning properly.
Here’s your current code:
function res($cur_category){
$children_categories = Mage::getModel('catalog/category')->getCategories($cur_category->getId());
$children_categories->addAttributeToFilter('name', array('like' => '%a%'));
foreach($children_categories as $child){
$name = $child->getName();
$option.='<li onClick="fill(\''.addslashes($child->getName()).'\');">' .$child->getName(). '</li>';
$option.=res($child);
}
unset($children_categories);
return $option;
}
function GetTree(){
$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
$categories->addAttributeToFilter('name', array('like' => '%a%'));
foreach($categories as $category)
{
$option.='<li onClick="fill(\''.addslashes($category->getName()).'\');">'.$category->getName().'</li>';
if($category->hasChildren()){
$option.= res($category);
}
}
unset($categories);
return $option;
}
echo $select = '<ul>'.GetTree().'</ul>';
The issue lies in the way you’re applying the LIKE filter. Let’s dive deeper and solve this.
Understanding Magento’s Category Filtering
Magento uses a core file named Mage_Catalog_Model_Resource_Category_Collection for category filtering. This collection applies some default filters that might be interfering with your LIKE query.
Specifically, by default, the collection applies filters like:
- Show only enabled categories
- Exclude hidden categories
These filters could be overriding your LIKE filter and causing it to fail.
The Solution: Overriding Magento’s Collection Filters
To resolve this issue, you’ll need to override the default category collection and remove or adjust the interfering filters. Here’s how:
-
Create a new file named
Mage_Catalog_Model_Resource_Category_Collection.phpin your local codepool (app/code/local/Mage/Catalog/Model/Resource). -
Copy the contents from the original
Mage_Catalog_Model_Resource_Category_Collection.phpfile located under your Magento installation’s core path. -
In this new file, find and comment out or delete these lines:
$this->addFieldToFilter('is_active', 1);
$this->addFieldToFilter('include_in_menu', array('neq' => 2));
-
Save the file and clear your cache (run
rm -rf var/cache/*). -
Now, in your original script that calls category collections, you can add any additional filters without issue:
$children_categories = Mage::getModel('catalog/category')->getCategories($cur_category->getId());
$children_categories->addAttributeToFilter('name', array('like' => '%a%'));
Conclusion: Solving Magento’s LIKE Query Issue
By overriding the default category collection and removing interfering filters, you’ve now resolved your LIKE query issue.
Remember, whenever you’re dealing with Magento collections, always check if there are any pre-applied filters that might be affecting your custom queries. Overriding the core files is a valid approach in such cases.
For more advanced Magento development tips like this one, feel free to explore our Magento Development category on LaravelCompany for deeper insights and best practices from experienced developers.
Happy coding!