2012-02-25 21 views
6

我希望能夠通過API調用以獲取包含URL密鑰等詳細信息的所有類別的數組。到底這個目標會是這樣得到所有類別的數組,包含Magento中的詳細信息

$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,但是這是基本上是目標。我確實需要我輸出的輸出。關於代碼實現需求的想法?

回答

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

So darn close .. $ url_path = $ category-> getUrl();是不是網址ie:我應該有基本配件/按摩油和工具.html我有http://domain.com/importScript.php/essential-accessories/massage-oils-and-tools。 html和甚至http://domain.com/importScript.php/catalog/category/view/s/in-the-shower/id/91/ – 2012-02-25 23:12:43

+0

啊,試試'$ url_path = $ category-> getUrlPath(); ' – seanbreeden 2012-02-25 23:46:58

+0

是我嘗試的第一件事情之一,但我得到了「/按摩油和工具」,我應該得到「按摩油和工具」貓的「基本配件/按摩油和工具」與父母「基本配件」.. – 2012-02-25 23:50:16

1

我在這裏寫功能UPTO三平返回數組形式

$陣列= hpCat(2,3); // categoryID,Sublevel upto three level

print_r($ array);

<?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(); 


?> 
+0

非常有幫助:) – 2016-06-25 06:49:33

0

爲了尋找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' 
) 
0

遞歸函數,使其:

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; 
} 

而且

$categoriesTree= $this->categoriesTree() 

我更喜歡使用對象比數組的節點模型,但你可以很容易地更換。

相關問題