Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Getting all products in categories, recursively, from a given parent
 
mrspeaker
Member
 
Total Posts:  44
Joined:  2009-01-19
 

I need to be able to return an array of categories, and its contained products as well as any sub-categories (also containing its products), from a given category id…

I have it working, by using

Mage::getResourceSingleton('catalog/category_tree')->load();
$root $tree->getNodeById($categoryId);
...
$collection Mage::getModel('catalog/category')->getCollection();
...
$tree->addCollectionData($collectiontrue);

To load the category tree, then in the _nodeToArray function loading in the products for each category:

$category Mage::getModel('catalog/category')->load($node->getId());
$result['product_count'$category->getProductCount();
$products = array();
        
// Load the products
foreach( $category->getProductCollection() as $product )
{
    $products[] 
$this->_getProductFromMagentoProduct$product );
}
$result[
'products'$products;
Which works… but seems like a pretty in-efficient way to do it. The _getProductFromMagentoProduct does some recursing of it’s own - I need to load the product to get it’s image urls and to load any simple products if it’s a bundled product.

Is there any built-in functionality to do this, or is this the best way?

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top