2012-02-25 5 views
6

Voglio essere in grado di chiamare attraverso l'API per ottenere una matrice di tutte le categorie con i dettagli come la chiave URL. Questo obiettivo, alla fine, sarà un array come questoottenere una matrice di tutte le categorie con dettagli in 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") 
); 

Così vorrei chiamare qualcosa come

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

So che è totalmente composto e non reale alle API, ma che è fondamentalmente l'obiettivo. Ho bisogno dell'output come l'ho mostrato. Idee sul codice per raggiungere l'esigenza?

risposta

20

Questo otterrà i vostri valori. Puoi costruire il tuo array come preferisci da qui.

$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

Ho adattato questo da un post su MagentoCommerce.com. È possibile utilizzare questo invece:

$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

Quindi dannatamente vicino .. $ url_path = $ category-> getUrl(); non è l'URL, cioè: dove avrei dovuto avere gli accessori-essenziali/massaggi-oli-e-strumenti.html che avevo http://domain.com/importScript.php/essential-accessories/massage-oils-and-tools. html o anche http://domain.com/importScript.php/catalog/category/view/s/in-the-shower/id/91/ –

+0

Ah, prova '$ url_path = $ category-> getUrlPath(); ' – seanbreeden

+0

È stata una delle prime cose che ho provato, ma ho ottenuto"/massage-oils-and-tools "dove avrei dovuto ottenere" essential-accessories/massage-oils-and-tools "per il gatto" Massage Oils and Tools " con il genitore "Accessori essenziali" .. –

1

QUI ho scritto FUNZIONE FINO A TRE LIVELLI Rientro in formato di array

$ array = hpCat (2,3); // categoryID, Sottolivello fino a tre livelli

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

Molto utile :) –

0

Per le persone in cerca di query MySQL per andare a prendere tutte le categorie di 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

Una funzione ricorsiva per rendere più:

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

E

$categoriesTree= $this->categoriesTree() 

Io preferisco usare oggetti di array di modellare un nodo, ma si può facilmente sostituire.