0

I'd like to retrive the categories tree excluding some categories of level 2. Here is my code where I exclude them

$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('id')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('is_active')
    ->addAttributeToFilter('level', array('eq'=>2))
    ->addAttributeToFilter('entity_id',array('nin'=>array($parent_id_to_exclude)))
    ->load()
;

Then I imagine to create a foreach loop to retrieve the subcategories, but I have no idea which is the attribute to select the parents. thanks

1

You can get child items by filtering on parent_id. Optionally there is a resource model Mage_Catalog_Model_Resource_Category_Tree that accepts a collection via method addCollectionData.

I've never used it to be honest but might help with building the category tree

|improve this answer|||||
  • using filter on parent_id, I retrive just the first level of a category. Any idea to get all the tree of a particular category? thanks – Antonio Pedicini Mar 16 '15 at 15:02
  • ->addAttributeToFilter('path', array('like'=> '%/{$parentId}/%')); might work. That will give all categories that fall under a certain category – Sander Mangel Mar 16 '15 at 15:03
  • many thanks, I did it using just one foreach loop and adding ->addAttributeToFilter('path', array('nlike'=> "%/$parents_to_exclude/%")) – Antonio Pedicini Mar 16 '15 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.