Magento 1.9:如何获得特定类别的子类别?

时间:2016-01-07 15:59:31

标签: php magento categories

我在Magento CMS中使用以下块作为主页:

{{block type="catalog/product_list" name="catalog_list" category_id="1420" template="catalog/product/listStart.phtml"}}

如何获得块中指定的category_id的所有子类别的输出(在本例中为id 1420)。

到目前为止,我有以下代码:

<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<div class="category-products">
<div id="carousel">
    <ul class="products-in-row">
        <?php $i=0; foreach ($collection as $cat): ?>
            <li class="item">
                <?php echo $cat->getName();?>
            </li>
        <?php endforeach ?>
    </ul>
</div>

我只获得主要类别的所有子类别。

1 个答案:

答案 0 :(得分:2)

如果您想获得每个当前类别的子类别

,此代码可能会有所帮助
  <?php
  $layer = Mage::getSingleton('catalog/layer');
  $_category = $layer->getCurrentCategory();
  $currentCategoryId= $_category->getId();
  $children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
  foreach ($children as $category)
  {
      echo $category->getName(); // will return category name 
      echo $category->getRequestPath(); // will return category URL
  }
  ?>

另一种方式:

<?php
  $categoryId = 10 ;  // get current category id
  $category = Mage::getModel('catalog/category')->load($categoryId);
  $catList = explode(",", $category->getChildren());
  foreach($catList as $cat)
  {
     $subcategory = Mage::getSingleton('catalog/category')->load($cat);
     echo $subcategory->getName(); 
  }
?>