How to get category name in Magento?

Im new to Magento. I have a "select-box" that lists all the main "category-names". How to get "Category Name" in Magento?

+4
source share
2 answers
<select> <?php $category = Mage::getModel('catalog/category'); $tree = $category->getTreeModel(); $tree->load(); $ids = $tree->getCollection()->getAllIds(); if ($ids) { foreach ($ids as $id) { $cat = Mage::getModel('catalog/category'); $cat->load($id); if($cat->getLevel()==1 && $cat->getIsActive()==1) { echo "<option>"; echo $cat->getName(); echo "</option>"; } } } ?> </select> 
+7
source

First enter Catalog-> Category helper:

 $helper = Mage::helper('catalog/category'); 

Location: application / code / kernel / Mage / Directory / Helper / Category .php

Then:

 <select> <?php foreach ($helper->getStoreCategories() as $_category): ?> <?php if ($_category->getIsActive()): ?> <option value="<?php echo $_category->getId(); ?>"><?php echo $_category->getName(); ?></option> <?php endif; ?> <?php endforeach; ?> </select> 

Note: This applies only to top-level categories. If you want to get child categories, then you can get them with something like:

 <?php if ($_category->hasChildren()): ?> <?php $category = Mage::getModel('catalog/category')->load($_category->getId()); ?> <?php foreach ($category->getChildrenCategories() as $subcategory): ?> <?php if ($subcategory->getIsActive()): ?> <?php echo $helper->getCategoryUrl($subcategory); ?> <?php echo $subcategory->getName(); ?> <?php /* etc... */ ?> <?php endif; ?> <?php endforeach; ?> <?php endif; ?> 
+5
source

Source: https://habr.com/ru/post/1484774/


All Articles