Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
get a list of all categories
 
catan
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2008-10-09
 

Hi, I am wondering if there is a way to get a list of all categories for ... a store?  I am new to magento.  I am programming a script that is to be run on the command line via a cron, and I want to sync categories from an incoming list.  Therefore I want to get a list of existing categories from magento so I know whether to add one as a new category or not.

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Discovery
Enthusiast
 
Total Posts:  767
Joined:  2007-12-29
 

Use the API. Create a web services user with full access, generate them a key then login from your script with that user/key. The methods for pulling the category information are all documented in the API, start pulling arrays back, dump them out and then work out how your custom app is to work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
catan
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2008-10-09
 

Yeah, I’ve seen that you can do something like that with a SOAP call to your web application...but why wouldn’t you be able to do that through the Mage classes?  I think that’s a point of confusion for me.  Is that just something overlooked in the class set?  Can you even load magento data, by name for instance ( like doing an sql search) via the Mage classes?

thanks though

 
Magento Community Magento Community
Magento Community
Magento Community
 
catan
Jr. Member
 
Avatar
Total Posts:  7
Joined:  2008-10-09
 

Well, after many var_dumps and get_class_methods calls, I have come up with some non-SOAP calls to get categories.  Not too efficient, but hope this helps…

require_once ‘app/Mage.php’;
Mage::app();

function get_categories(){

$category = Mage::getModel(’catalog/category’);
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();
$arr = array();

if ($ids){
foreach ($ids as $id){
$cat = Mage::getModel(’catalog/category’);
$cat->load($id);
array_push($arr, $cat);
}
}

return $arr;

}

 
Magento Community Magento Community
Magento Community
Magento Community
 
eiocreative
Jr. Member
 
Total Posts:  21
Joined:  2009-01-24
 

require_once $_SERVER[’DOCUMENT_ROOT’]."/app/Mage.php";
Mage::app(’1’);

function nodeToArray(Varien_Data_Tree_Node $node)
{
$result = array();
$result[’category_id’] = $node->getId();
$result[’parent_id’] = $node->getParentId();
$result[’name’] = $node->getName();
$result[’is_active’] = $node->getIsActive();
$result[’position’] = $node->getPosition();
$result[’level’] = $node->getLevel();
$result[’children’] = array();

foreach ($node->getChildren() as $child) {
$result[’children’][] = nodeToArray($child);
}

return $result;
}

function load_tree() {

$tree = Mage::getResourceSingleton(’catalog/category_tree’)
->load();

$store = 1;
$parentId = 1;

$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($store)
->addAttributeToSelect(’name’)
->addAttributeToSelect(’is_active’);

$tree->addCollectionData($collection, true);

return nodeToArray($root);

}

function print_tree($tree,$level) {
$level ++;
foreach($tree as $item) {
echo str_repeat("    ", $level).$item[’name’]."<br>";
print_tree($item[’children’],$level);

}
}

$tree = load_tree();
print_tree($tree[’children’],0);

 
Magento Community Magento Community
Magento Community
Magento Community
 
Umid
Jr. Member
 
Total Posts:  1
Joined:  2009-08-24
 

catan,
thank you very much - you just saved me huge time. I have spent many hours trying to find good way of getting categories for home page content - and yours works like a charm

 
Magento Community Magento Community
Magento Community
Magento Community
 
ericp
Jr. Member
 
Total Posts:  17
Joined:  2009-06-15
 

Thank you.

I’m trying to get the url path for each category also.
I tried :
$result[’url’] = $node->getCategoryUrl($result[’category_id’]);
and
$result[’url’] = $node->getUrl();
But it’s empty.

If I use :
$result[’url’] = $node->getData(’path’);

I get [url] => 1/3/4

I need the html file name.

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
ericp
Jr. Member
 
Total Posts:  17
Joined:  2009-06-15
 

I got it.

In this section of code I added the url_path part:

$collection Mage::getModel('catalog/category')->getCollection()
            ->
setStoreId($store)
            ->
addAttributeToSelect('name')
            
[color=green]->addAttributeToSelect('url_path')[/color]
            
->addAttributeToSelect('is_active');

And in the nodeToArray method I added this:
$result[’url_path’] = $node->getData(’url_path’);

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagentoKing
Jr. Member
 
Total Posts:  12
Joined:  2009-01-10
 

@eiocreative

Hi
I have being through the code and this is very interesting to get the list of categories is simplifies my work . But can some body tells me where does all this code goes to get the expected result

 
Magento Community Magento Community
Magento Community
Magento Community
 
Appmerce
Guru
 
Avatar
Total Posts:  376
Joined:  2008-05-13
Amsterdam, The Netherlands
 

It works, but how to filter by current store?

I tried $store = Mage::app()->getStore()->getId();

However, the $collection isnt really filtered.

$collection = Mage::getModel(’catalog/category’)->getCollection()->setStoreId($store)->addAttributeToSelect(’url_path’)->addAttributeToSelect(’name’)->addAttributeToSelect(’is_active’);

Still doesnt filter by store…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Martin H
Member
 
Total Posts:  36
Joined:  2009-06-26
Buenos Aires, Argentina
 

Great post! Thanks!
Interested too in Morningtime question…
I would like to iterate over all the stores to show all the categories.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sergiosinicin
Jr. Member
 
Total Posts:  4
Joined:  2010-04-07
 

Could you please advise how the returned array $arr can get the structure as follows? Thx in advance.
array (
name = > category_id
)

require_once ‘app/Mage.php’;
Mage::app();

function get_categories(){

$category = Mage::getModel(’catalog/category’);
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();
$arr = array();

if ($ids){
foreach ($ids as $id){
$cat = Mage::getModel(’catalog/category’);
$cat->load($id);
array_push($arr, $cat);
}
}
return $arr;
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
sergiosinicin
Jr. Member
 
Total Posts:  4
Joined:  2010-04-07
 

I`ve found solutions:

function get_categories(){

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

}

return $arr;

}

  
  $arr 
=  get_categories();
 
$arr array_flip($arr);
 echo 
"<pre>";
var_dump($arr);
echo 
"</pre>";

?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
rreece0113
Sr. Member
 
Total Posts:  76
Joined:  2009-01-28
Oregon
 

To answer the original question of this post:

I made this little video on how I create custom category listing blocks with Magento.
I am sure there are better ways of achieving this or even something I could have done better, but it’s just my method. I only created this it in hopes that it helps explain somethings to some people out there.

Magento Custom Category Listing Tutorial

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
michele benvenuti
Member
 
Total Posts:  53
Joined:  2009-10-08
 

hi devinrayolsen, hi everybody,

I saw your method on your video, could you please paste your code here ?

I’m using this

<?php //if(count($this->getStoreCategories())>1): ?>
<ul id="general_catalog">
<?php $i=1; foreach ($this->getStoreCategories() as $_category): ?>
<?php echo $this->drawItem($_category, ' item-'.$i) ?>
<?php $i++; if ($i>9): $i=1; endif; endforeach ?>
</ul>
<?php //endif; ?>

that works, BUT my problem is to get All categoris, not by store view.
is it your code the right one ? ?

thanks.
mic b.

 
Magento Community Magento Community
Magento Community
Magento Community
 
anoopchris
Jr. Member
 
Total Posts:  1
Joined:  2009-02-11
Kochi
 

a simple code to get all the category names..

$collection= Mage::getModel(’catalog/category’)->getCollection()
->addAttributeToSelect(’name’)
->addAttributeToSelect(’is_active’);

foreach($collection as $col)
{

$col->getName();

}

you can get all the details of a category from its supporting functions..for a list of functions for category, use the following code…

echo ‘<pre>’;
print_r(get_class_methods($collection));
echo ‘</pre>’;

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2