-
Categories
-
Tags
attributes authorize.net awards category list Code code formatting CSS custom attributes debug debugging design featured sites Free Theme great web design articles HTML jQuery jQuery Tabs magento magento development magento product attributes News payment methods php Post_ Premium Wordpress Theme product grid product list programming recognition sessions Web Design Web Design Resources web development wordpress Wordpress Theme -
archives
Magento Categories outside of Magento
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 Code, Magento and tagged category list, magento, magento development, web development. Bookmark the permalink.
By outside of Magento installation do you mean outside of the Magento directory?
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