2017-09-01 120 views
0

我製作了一個小腳本,用於循環產品頁面上某個產品所屬的所有類別。就像這樣:Magento產品頁面上的循環類別名稱及其子類名稱

enter image description here

我的問題是,所有類別都在一個大的名單顯示,而他們都來自一定量的最高類別。例如,對於所示的圖像,所有類別來自allergenenintolerantiesvoedingsstijlen。客戶沒有看到這一點。

我想分裂列表和顯示前categorie子類都屬於這樣的:

enter image description here

所以我想我首先需要獲得從類別中的所有parentids然後用這些ID我需要獲得所有頂級的分類名稱,循環它們,並在該循環內循環子類別。

我該怎麼做magento結構化代碼? 第一個圖像顯示用下面的代碼:

<?php 
// Haal alle categorieen op waar het product onder valt 
$currentCatIds  = $_product->getCategoryIds(); 
$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
->addAttributeToSelect('name') 
->addAttributeToSelect('url') 
->addAttributeToFilter('entity_id', $currentCatIds) 
->addIsActiveFilter(); 


$sorted = sort($categoryCollection); 

$catlist .= '<ul class="categorielijst">'; 
foreach ($categoryCollection as $cat) { 
    if ($cat->getName() != 'Root') { 
     $catlist .= ' 
     <li><a href="' . $cat->getUrl() . '">' . $cat->getName() . '</a></li>'; 
    } 
} 
$catlist .= '</ul>'; 
echo $catlist; 
?> 

回答

0
$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
->addFieldToFilter('level',2) 
->addAttributeToSelect('name') 
->addAttributeToSelect('url'); 
$out = "<ul>"; 
    foreach($categoryCollection as $cat){ 
     $children = $cat->getChildren(); 
     $out .= "<li>"; 
     $out .= "<a href='".$cat->getUrl()."'>".$cat->getName()."</a>"; 
      $out .="<ul class='sub'>"; 
      foreach($children as $child){ 
       $ChildModel = Mage::getModel('catalog/category')->load($child); 
       $out .="<li><a href='".$ChildModel->getUrl()."'>".$ChildModel->getName()."</a></li>"; 
      } 
      $out .="</ul>"; 
     $out .= "</li>"; 
    } 
    $out .= "</ul>"; 
    echo $out; 
+0

是對Magento的1.9這個正確的代碼?只有使用此代碼加載的頂級類別,而不是子級

    爲空。 – twan

    +0

    它的工作在我的一端,你只需要從你可以獲得所有主要類別(addFieldToFilter('level',2))的第二級類別,然後你可以通過Mage :: getModel獲得所有它的子類別(' catalog/category') - > load( - CHILDREN ID--); –

    相關問題