Search code examples
phpmagento

How to get sub-categories of two parent category


I had a main category (parent category) whose id = 5 & 37. I want to collection of its sub-categories. How can I do that?

$catid = array(5,37);


$_category = Mage::getModel('catalog/category')->load(5);
$_subcategories1 = $_category->getChildrenCategories();
$_category = Mage::getModel('catalog/category')->load(37);
$_subcategories2 = $_category->getChildrenCategories();  

i want the collection which have children categories from both category id(5,37)


Solution

  • You can get that from one select:

    $subcategories = Mage::getModel('catalog/category')
        ->setStoreId(Mage::app()->getStore()->getId())
        ->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('parent_id', array(5, 37))
        ->setOrder('parent_id', 'ASC');//if you want them somehow grouped by parent_id
    foreach ($subcategories as $category){
        //do something with $category
    }