获取指定product category id 自身及其所有子类信息
/* Retrieve category collection tree
*
* @param int $parent
* @param string|int $store
* @return array
*/
public function collectiontree($parentId = null, $store = null)
{
if (is_null($parentId) && !is_null($store)) {
$parentId = Mage::app()->getStore($this->_getStoreId($store))->getRootCategoryId();
} elseif (is_null($parentId)) {
$parentId = 1;
}
/* @var $tree Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree */
$tree = Mage::getResourceSingleton('catalog/category_tree')
->load();
$root = $tree->getNodeById($parentId);
if($root && $root->getId() == 1) {
$root->setName(Mage::helper('catalog')->__('Root'));
}
$collection = Mage::getModel('catalog/category')->getCollection()
->setStoreId($this->_getStoreId($store))
->addAttributeToSelect('mobile_background_image')
->addAttributeToSelect('mobile_thumbnail_image')
->addAttributeToSelect('mobile_background_image_blur')
->addAttributeToSelect('name')
->addAttributeToSelect('updated_at')
->addAttributeToSelect('english_title')
->addAttributeToSelect('description')
->addAttributeToSelect('is_active');
$tree->addCollectionData($collection, true);
//error_log("\n----\$root ------\n".var_export($root->getData(),true),3,Mage::getBaseDir().'/var/log/categoryTree.log');
return $this->_collectionNodeToArray($root);
}
/* Convert node to array
*
* @param Varien_Data_Tree_Node $node
* @return array
*/
protected function _collectionNodeToArray(Varien_Data_Tree_Node $node)
{
$mediaDir = "/media/catalog/category/";
// Only basic category data
$result = array();
$result['category_id'] = $node->getId();
$result['parent_id'] = $node->getParentId();
$result['name'] = $node->getName();
$result['updated_at'] = $node->getUpdatedAt();
$result['is_active'] = $node->getIsActive();
$result['position'] = $node->getPosition();
$result['level'] = $node->getLevel();
$result['mobile_thumbnail_image'] = $node->getMobileThumbnailImage() ? $mediaDir.$node->getMobileThumbnailImage():'';
$result['mobile_background_image'] = $node->getMobileBackgroundImage() ? $mediaDir.$node->getMobileBackgroundImage():'';
$result['mobile_background_image_blur'] = $node->getMobileBackgroundImageBlur() ? $mediaDir.$node->getMobileBackgroundImageBlur() : '';
$result['english_title'] = $node->getEnglishTitle();
$result['description'] = strip_tags($node->getDescription());
$result['children'] = array();
foreach ($node->getChildren() as $child) {
$result['children'][] = $this->_collectionNodeToArray($child);
}
return $result;
}