【问题标题】:Magento 1.9: How to get Subcategories of specific Category?Magento 1.9:如何获取特定类别的子类别?
【发布时间】:2016-01-07 15:59:31
【问题描述】:

我在我的 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>

我只获取主类别的所有子类别。

【问题讨论】:

  • @DouglasRadburn - 不同的问题。据我所知,这个问题中的这个问题无法将值 1420category_id 应用到集合中,而不是一般如何去做。这也是与您链接的问题不同的情况。

标签: php magento categories


【解决方案1】:

如果您想获取每个当前类别的子类别,此代码可能会有所帮助

  <?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(); 
  }
?> 

【讨论】:

  • 我在你的第二个例子中修正了一些错别字,它对我有用。我认为这会对我有所帮助。谢谢。
  • 我认为这没有考虑子类别的顺序...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 2011-07-30
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多