Magento Expert Forum - Improve your Magento experience
-
.How to filtrate Magento category collection by rank?
Sometimes you need to get a list of all categories with a particular level for a custom navigation menu. This can be done as follows:
$model=Mage::getModel('catalog/category');
$categories=$model->getCollection()->addLevelFilter(2)
->addAttributeToSelect('*')->addIsActiveFilter();
However, this is not always correct. I think I should explain why, right?
If I will enter request to a base I will get the following: SELECT `main_table`.* FROM `catalog_category_flat_store_1` AS `main_table` WHERE (main_table.level <= 2) AND (is_active = ’1′)
It is very visible that condition <= is in use, what doesn’t fit, because filtering categories of 3 rd level we will also get categories with 2 nd level.
Due to this we should do filtration in the following way:
$categories=$model->getCollection()->addAttributeToFilter('level',2)
->addAttributeToSelect('*')->addIsActiveFilter();
After this we can do whatever we want with the collection.
Any questions? Ask here
View more threads in the same category:
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks