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

时间:2012-02-25 21:18:40

标签: php magento e-commerce

我希望能够通过API调用以获取所有类别的数组,其中包含URL键等详细信息。最终的目标将是像这样的数组

$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")
);

所以我想说出像

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

我知道这完全是弥补而不是真实的API,但这基本上就是目标。我确实需要输出。想法在代码上实现需要吗?

4 个答案:

答案 0 :(得分:20)

这将获得您的价值观。你可以从这里建立阵列。

$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

我在MagentoCommerce.com的帖子中对此进行了调整。您可以改为使用它:

$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();
    }
}

答案 1 :(得分:1)

这里我的WROTE功能上升到三个级别返回阵列格式

  

<强> $阵列= hpCat(2,3); // categoryID,Sublevel最多三级

     

<强>的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();


?>

答案 2 :(得分:1)

为寻找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'
)

答案 3 :(得分:0)

一个递归函数:

thead

并且

.table-bordered td,
.table-bordered th {
  border: none !important;
  border-right: solid 1px #ccc !important;
}

我更喜欢使用对象而非数组来建模节点,但您可以轻松替换。