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
$root = $tree->getNodeById($categoryId);
$collection = Mage::getModel('catalog/category')->getCollection();
To load the category tree, then in the _nodeToArray function loading in the products for each category:
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.
$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;
Is there any built-in functionality to do this, or is this the best way?