load categories outside magento 8 comments


Code 1 : simple code to show ul li

<?php
    require_once "app/Mage.php";
    umask(0);
    Mage::app();
    Mage::app()->getTranslator()->init('frontend');
    Mage::getSingleton('core/session', array('name' => 'frontend'));

    // switch off error reporting
    error_reporting ( E_ALL & ~ E_NOTICE );
    // Brands category => ID = 101
    $brand    = Mage::getModel('catalog/category')->load('2');//root category
    $brands     = $brand->getChildrenCategories();
?>    
<div class="brands">
    <h2>Our Categories</h2>
    <ul>
<?php
    foreach ($brands as $item):
        // get class name by removing diacritics
        $class     = strtolower( preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format( $item['name'] ) ) );
        // get category URL
        $url  = Mage::getModel('catalog/category')->load($item['id'])->getUrl();
?>
        <li>
            <a class="<?php echo $class; ?>" href="<?php echo $url; ?>" title="Show all products of <?php echo $item['name']; ?> brand">
                <span>
                    <?php echo $item['name']; ?>
                </span>
            </a>
        </li>
<?php endforeach; ?>
    </ul>

</div>

Thanks to : http://www.magentocommerce.com/boards/viewthread/78240/

Code 2 : Using attribute to select

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
Mage::app('1');
$collection= Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('id')
->addAttributeToSelect('parent_id')
->addAttributeToSelect('position')
->addAttributeToSelect('level')
->addAttributeToSelect('is_active');
echo "<table border='1' width='100%'>";
    echo "<tr>";
    echo "<th>id</th>";
    echo "<th>name</th>";
    echo "<th>parent</th>";
    echo "<th>is_active</th>";
    echo "<th>position</th>";
    echo "<th>level</th>";
echo "</tr>";

foreach($collection as $col)
{
    echo "<tr>";
            echo "<td>".$col->getId()."</td>";
            echo "<td>".$col->getName()."</td>";
            echo "<td>".$col->getParentId()."</td>";
            echo "<td>".$col->getIsActive()."</td>";
            echo "<td>".$col->getPosition()."</td>";
            echo "<td>".$col->getLevel()."</td>";
    echo "</tr>";
}
echo "</table>";
?>

Thanks to : http://www.magentocommerce.com/boards/viewthread/24947/

Code 3 : using various functions

<?php
//http://www.magentocommerce.com/boards/viewthread/24947/P15/
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('id')
        ->addAttributeToSelect('is_active');
        $tree->addCollectionData($collection, true);

    return nodeToArray($root);
}
function print_tree($tree, $level) {
    $level++;
    foreach ($tree as $item) {
        echo "<tr>";
        echo "<td>" . str_repeat("*", $level) . "</td>";
        echo "<td>" . $item['category_id'] . "</td>";
        echo "<td>" . $item['name'] . "</td>";
        echo "<td>" . $item['parent_id'] . "</td>";
        echo "</tr>";
      
        print_tree($item['children'], $level);
    }
}
$tree = load_tree();
echo "<table border='1' width='100%'>";
echo "<tr>";
echo "<td colspan='4'>";
echo "<table border='1' width='100%'>";
echo "<tr>";
echo "<th>level</th>";
echo "<th>category_id</th>";
echo "<th>name</th>";
echo "<th>parent_id</th>";
echo "</tr>";
print_tree($tree['children'], 0);
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
?>

Thanks to : http://www.magentocommerce.com/boards/viewthread/24947/P15/

Code 4 : using one function

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

function get_categories() {
    $category = Mage::getModel('catalog/category');
    $tree = $category->getTreeModel();
    $tree->load();
    $ids = $tree->getCollection()->getAllIds();
    //  echo "<pre>";
    //  print_r($ids);//getting all ids from magento
    //  echo "</pre>";
    $arr = array();
    if($ids) {
        foreach ($ids as $id) {
            $cat = Mage::getModel('catalog/category');
            $cat->load($id);
            //  $arr[$id] = $cat->getId(); //original
            $arr[$id]['category_id'] = $cat->getId();
            $arr[$id]['parent_id'] = $cat->getParentId();
            $arr[$id]['name'] = $cat->getName();
            $arr[$id]['is_active'] = $cat->getIsActive();
            $arr[$id]['position'] = $cat->getPosition();
            $arr[$id]['level'] = $cat->getLevel();
        }// for each ends
        echo "<pre>";
        print_r($arr);
        echo "</pre>";
    }//if ids present
   // return $arr;
}
get_categories();
?>

Code 5 : Export parameters to CSV

<?php
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
if ($ids) {
    $file = "var/import/catwithid.csv";
    file_put_contents($file, "catId,catName,catParent_id,catLevel\n");//two more fields
    foreach ($ids as $id) {
        $pk = Mage::getModel('catalog/category');
        $pk->load($id);
        $string = $pk->getId() . ',' . $pk->getName() . ',' . $pk->getParentId() . ',' . $pk->getLevel() . "\n";
        file_put_contents($file, $string, FILE_APPEND);
    }
}
?>

Code 6 : From magento core database table

Tested in V 1.5.1.0 : Work in progress : query is as below

<?php
SELECT a.entity_id,b.value,a.parent_id,a.level,a.children_count,a.position
FROM
catalog_category_entity_varchar b,catalog_category_entity a
WHERE
a.entity_type_id = 3 and
b.attribute_id = 33 and
a.entity_id = b.entity_id
order by a.entity_id , a.level
?>

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools


8 thoughts on “load categories outside magento