简体   繁体   English

在Magento中获取所有类别的数组

[英]get an array of all categories with details in Magento

I want to be able to call through the API to get an array of all the categories with the details like the URL key. 我希望能够通过API调用以获取所有类别的数组,其中包含URL键等详细信息。 That goal in the end will be an array like this 最终的目标将是这样的阵列

$massage_cats=array(
    array("entity_id"=>78,
          "name"=>"Massage Oils and Tools",
          "url_key"=>"massage-oils-and-tools",
          "url_path"=>"essential-accessories/massage-oils-and-tools.html"),
    array("entity_id"=>79,
          "name"=>"Massage Oils",
          "url_key"=>"massage-oils",
          "url_path"=>"essential-accessories/massage-oils-and-tools/massage-oils.html")
);

So I would want to call out something like 所以我想说出类似的东西

$massage_cats= array();
$allcats = Mage::getModel('catalog/cats?')->loadAll();
    foreach($allcats $k=>$item){
        array_push($massage_cats,$item->loadDetails());
    }

I know that is totally made up and not real to the API but that is basically the goal. 我知道这完全是弥补而不是真正的API,但这基本上是目标。 I do need the output as I showed it. 我确实需要输出。 Ideas on the code to achieve the need? 关于代码实现需求的想法?

This will get your values. 这将获得您的价值观。 You can build your array however you like from here. 你可以从这里建立你的阵列。

$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToSelect('is_active');

foreach ($categories as $category)
{
    if ($category->getIsActive()) { // Only pull Active categories
        $entity_id = $category->getId();
        $name = $category->getName();
        $url_key = $category->getUrlKey();
        $url_path = $category->getUrl();
    }
}

EDIT

I adapted this from a post on MagentoCommerce.com . 我从MagentoCommerce.com上的帖子改编了这个。 You can use this instead: 您可以使用此代替:

$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
if ($ids){
    foreach ($ids as $id){
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);

        $entity_id = $cat->getId();
        $name = $cat->getName();
        $url_key = $cat->getUrlKey();
        $url_path = $cat->getUrlPath();
    }
}

HERE I WROTE FUNCTION UPTO THREE LEVELS RETURN IN ARRAY FORMAT 在这里我WROTE功能上升到三个级别返回阵列格式

$array=hpCat(2,3); $阵列= hpCat(2,3); //categoryID,Sublevel upto three level // categoryID,Sublevel最多三级

print_r($array); 的print_r($阵列);

<?php     

function hpCat($id,$level=0){
    if(!empty($id)){
        $level=empty($level)?0:$level;
        $category = Mage::getModel('catalog/category')->load($id);
        $levelOneItems = $category->getChildrenCategories();
        if (count($levelOneItems) > 0){
            $array=hpCatDetails($category);
            if($level>=1):
                $i=0;
                foreach($levelOneItems as $levelOneItem){ 
                    $array['sub'][$i]=hpCatDetails($levelOneItem);    
                    $leveltwoItems=$levelOneItem->getChildrenCategories();
                    if (count($leveltwoItems) > 0){
                        if($level>=2):
                            $j=0;
                            foreach($leveltwoItems as $leveltwoItem){     
                                $array['sub'][$i]['sub'][$j]=hpCatDetails($leveltwoItem);
                                $levelthreeItems=$leveltwoItem->getChildrenCategories();
                                if (count($levelthreeItems) > 0){
                                    if($level>=3):
                                    $k=0;
                                    foreach($levelthreeItems as $levelthreeItem){     
                                        $array['sub'][$i]['sub'][$j]['sub'][$k]=hpCatDetails($levelthreeItem);
                                        $k++;
                                    }  
                                    endif;
                                }
                                $j++;
                            }  
                        endif;
                    }
                    $i++;
                }
            endif;
        }
        return $array;
    }
    return array();
}
function hpCatDetails($cat){
    return array('name'=>$cat->getName());
}

$array=hpCat(2,3);//categoryID,Sublevel upto three level
echo '<pre>';print_r($array);die();


?>

For the people looking for MySQL query to fetch all Magento categories. 对于寻找MySQL查询以获取所有Magento类别的人。

SELECT
    e.entity_id AS id,
    e.parent_id,
    e.path,
    e.`level`,

IF (
    at_name.value_id > 0,
    at_name.
VALUE
    ,
    at_name_default.
VALUE

) AS `name`
FROM
    `catalog_category_entity` AS `e`
INNER JOIN `catalog_category_entity_varchar` AS `at_name_default` ON (
    `at_name_default`.`entity_id` = `e`.`entity_id`
)
AND (
    `at_name_default`.`attribute_id` = '41'
)
LEFT JOIN `catalog_category_entity_varchar` AS `at_name` ON (
    `at_name`.`entity_id` = `e`.`entity_id`
)
AND (
    `at_name`.`attribute_id` = '41'
)

A recursive function to make it : 一个递归函数:

 private function __categoriesTree($id = 2) {
    $category = Mage::getModel('catalog/category');

    $_category = $category->load($id);
    $details = new stdClass();
    list($details->id, $details->name, $details->urlKey, $details->level, $details->children) = [
        $_category->getId(),
        $_category->getName(),
        $_category->getUrlKey(),
        $_category->getLevel(),
        []
    ];

    foreach (array_filter(explode(',', $_category->getChildren())) as $childId) {
        $details->children[] = $this->__categoriesTree($childId);
    }
    if (count($details->children) === 0) {
        unset($details->children);
    }
    return $details;
}

And

$categoriesTree=  $this->categoriesTree()

I prefer to use objects than arrays to model a node but you can easily replace. 我更喜欢使用对象而不是数组来建模节点,但您可以轻松替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM