Skip to content

Instantly share code, notes, and snippets.

@piotrbelina
Created March 1, 2013 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piotrbelina/5065368 to your computer and use it in GitHub Desktop.
Save piotrbelina/5065368 to your computer and use it in GitHub Desktop.
Magento categories export
<?php
/**
* Created by JetBrains PhpStorm.
* User: piotr
* Date: 01.03.13
* Time: 16:07
* To change this template use File | Settings | File Templates.
*/
require_once __DIR__ . '/../../../app/Mage.php';
Mage::app('admin');
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['code'] = $node->getCode();
$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('code')
->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'];
echo str_repeat(';', 5-$level);
echo $item['code'];
echo "\n";
print_tree($item['children'],$level);
}
}
$tree = load_tree();
print_tree($tree['children'],-1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment