4

我是 Magento 的新手。我有一个“选择框”,列出了所有主要的“类别名称”。如何在 Magento 中获取“类别名称”?

4

2 回答 2

7
<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>
于 2013-06-07T05:50:27.327 回答
5

首先获取Catalog->Category helper:

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

位置:app/code/core/Mage/Catalog/Helper/Category.php

然后:

<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>

注意:这仅适用于顶级类别。如果您也想获得子类别,那么您可以通过以下方式获得它们:

<?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; ?>
于 2013-06-06T07:30:26.160 回答