magento custom category api (1) --- collectiontree

本文介绍了一个用于获取指定产品类别ID及其所有子类信息的方法。该方法通过构建类别树并选择性地加载属性来实现,最终返回一个包含所需信息的数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取指定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;
    }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值