blog

Magento Categories outside of Magento

published in

Magento

comments

4

In some cases you will need to bring a list of Magento’s categories outside of the Magento installation and into either a static page or custom solution. This script will allow you to define the parent category and print out a list of child categories that are active in Magento.

In the top of every file you are rendering a dynamic category menu from Magento you will need to include Mage at the very top with no empty lines above it:

// Include Mage
// Change to Mage.php as needed
include_once '/Applications/MAMP/htdocs/test_sites/magento/app/Mage.php';
Mage::app();

If you dont know the exact server path to Mage.php, go ahead and include the above and refresh the page, PHP should produce an error telling you the path needed to at least the directory of Magento on the server.

From here, where you want to output an unordered list of categories with links place this script:

// Get List of categories
function nodeToArray(Varien_Data_Tree_Node $node) { 

        $result = array();
        $result['category_id'] = $node->getId();
        $result['parent_id'] = $node->getParentId();
        $result['url_path'] = $node->getData('url_path');
        $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(); 

        // Set this to your store ID (usually 1 unless using multi-store)
        $store = 1;

        // Set this to the ID of the root category you are using
        $parentId = 3; 

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

        $root = $tree->getNodeById($parentId);

        // Set this to match $parentId
        if($root && $root->getId() == 3) { 

                $root->setName(Mage::helper('catalog')->__('Root')); 

        }

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

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

        return nodeToArray($root);

}

function print_tree($tree,$level) { 

        $level ++; 

        // Print out each item for the nav
        foreach($tree as $item) {

                print '<li><a href="store/'.$item['url_path'].'">';
                echo str_repeat("    ", $level).$item['name'];
                print '</a></li>';
                print_tree($item['children'],$level); 

        } 

}

$tree = load_tree(); 

// Print the Category Nav
print_tree($tree['children'],0);

On line 29 you will need to set your store ID. This will probably be 1 unless you are running a multiple store configuration. On line 32 you will need to set the ID of the parent category you want the list to pull from. If you navigate to the category in the admin panel of Magento the ID will display in parenthesis next to the category name.

If you need to exclude a category from the list replace lines 64-67 with:

// If you want to exclude a category
// Add your ID in place of 6
if ($item['category_id'] != '6' ) { 

        print '<li><a href="store/'.$item['url_path'].'">';
        echo str_repeat("    ", $level).$item['name'];
        print '</a></li>';
        print_tree($item['children'],$level); 

}

You will need to change the number 6 on line 3 to be the ID of the category that you want to exclude. If you need to exclude multiple categories add an OR statement into line 3 above for each category that needs to be excluded.

This entry was posted in Magento and tagged , , , . Bookmark the permalink.

4 Responses to Magento Categories outside of Magento

  1. Jason says:

    By outside of Magento installation do you mean outside of the Magento directory?

  2. admin says:

    It can be outside of the Magento directory, or if you installed some CMS inside the Magento directory (such as /blog) it would work there as well.

    Bret

  3. andrew says:

    Works great! Thanks dude. I owe you a beer for this.

  4. BretJG says:

    Glad it helped! Cheers for sure.

    Bret

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>