Reputation: 2443
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)
Upvotes: 1
Views: 1298
Reputation: 15206
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
}
Upvotes: 2
Reputation: 11853
here i am giving you one example to merge two collection of category in to one collection
$storeId = Mage::app()->getStore()->getId();
$categoryOneId = 5;
$categoryTwoId = 37;
$categoryOne = Mage::getModel('catalog/category')->load($categoryOneId);
$categoryTwo = Mage::getModel('catalog/category')->load($categoryTwoId);
$collectionOne = Mage::getModel('catalog/product')->getCollection()
->setStoreId($storeId)
->addCategoryFilter($categoryOne);
$collectionTwo = Mage::getModel('catalog/product')->getCollection()
->setStoreId($storeId)
->addCategoryFilter($categoryTwo);
$merged_ids = array_merge($collectionOne->getAllIds(), $collectionTwo->getAllIds());
$mergedCollection = Mage::getModel('catalog/product')->getCollection()
->addFieldToFilter('entity_id', $merged_ids);
hope this will sure help you.
Upvotes: 0