2016-01-07 105 views
2

我使用以下塊在我的Magento CMS產品網站:Magento 1.9:如何獲取特定類別的子類別?

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

如何爲(在這種情況下編號1420)在塊中指定我能不能得到CATEGORY_ID的所有子類別的輸出。

到目前爲止,我有以下代碼:

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

我只得到了主要類別的所有子類別。

+0

的可能的複製[Magento的 - 得到一個父類和所有子子類別](http://stackoverflow.com/questions/5564647/magento-get-a-parent-category-and-all-sub-sub-categories) –

+0

@DouglasRadburn - 不同的問題。就我所知,這個問題中的這個問題無法將從'category_id'中的值'1420'應用到集合中,而不是一般如何執行。對於您所鏈接的問題來說,這也是一個不同的場景。 –

回答

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

我在第二個例子中修正了一些拼寫錯誤,它適用於我。我認爲這會對我有幫助。謝謝。 –

+0

我認爲這不考慮子類別的順序... – Wouter

相關問題