2017-09-29 62 views
0

我想显示产品所属类别的列表,顶级类别和子类别。如何仅加载属于当前产品的子类别(magento)

我想出了如何加载正确的topcategories,但是当我循环subcats(子类)时,它会加载该topcategory的所有子对象,而不是该产品所属的类别。

例子:

enter image description here

就像你可以看到它装入一吨的子类别,但只有与红色条纹的那些是产品处于子类别。

我怎样才能确保它只显示在他们的topcategories下的那些?

我的代码:

$currentCatIds = $_product->getCategoryIds(); 
    $categoryCollection = Mage::getResourceModel('catalog/category_collection') 
->addAttributeToSelect('name') 
// ->addFieldToFilter('level',2) 
->addAttributeToSelect('url') 
->addAttributeToFilter('entity_id', $currentCatIds) 
->addIsActiveFilter(); 

    $out = "<ul>"; 
    foreach($categoryCollection as $cat){ 
     $out .= "<li>"; 
     $out .= "<b><a href='".$cat->getUrl()."'>".$cat->getName()."</a></b>"; 
     $out .="<ul class='sub'>"; 
     $children = Mage::getModel('catalog/category') 
     // ->addAttributeToFilter('entity_id', $cat->getCategoryIds()) 
     ->load($cat->getId()) 
     ->getChildrenCategories(); 
      foreach($children as $child){ 
       $out .="<li><a href='".$child->getUrl()."'>".$child->getName()."</a></li>"; 
      } 
      $out .="</ul>"; 
     $out .= "</li>"; 
    } 
    $out .= "</ul>"; 
    echo $out; 

回答

0

在你的循环检查,如果有类产品或者不是。 if($cat->getProductCount()){..}和同样的事情$child

$currentCatIds = $_product->getCategoryIds(); 
$categoryCollection = Mage::getResourceModel('catalog/category_collection') 
->addAttributeToSelect('name') 
// ->addFieldToFilter('level',2) 
->addAttributeToSelect('url') 
->addAttributeToFilter('entity_id', $currentCatIds) 
->addIsActiveFilter(); 

$out = "<ul>"; 
foreach($categoryCollection as $cat){ 
    if($cat->getProductCount()){ 
     $out .= "<li>"; 
     $out .= "<b><a href='".$cat->getUrl()."'>".$cat->getName()."</a></b>"; 
     $out .="<ul class='sub'>"; 
     $children = Mage::getModel('catalog/category') 
     // ->addAttributeToFilter('entity_id', $cat->getCategoryIds()) 
     ->load($cat->getId()) 
     ->getChildrenCategories(); 
      foreach($children as $child){ 
      if($child->getProductCount()){ 
       $out .="<li><a href='".$child->getUrl()."'>".$child->getName()."</a></li>"; 
      } 
      } 
      $out .="</ul>"; 
     $out .= "</li>"; 
} 
} 
$out .= "</ul>"; 
echo $out; 
0

请尝试我的代码,它将工作。

<?php 
    $currentCatIds = $_product->getCategoryIds(); 
    foreach ($currentCatIds as $categoryid) { 
      $sub_categories = Mage::getModel('catalog/category')->load($categoryid)->getChildrenCategories(); 
      foreach ($sub_categories as $category) { 
       $cat_details = Mage::getModel('catalog/category')->load($category->getId()); 
        ?> 
        <div class="col-sm-15 col-xs-4"> 
         <div class="catImage"> 
          <a href="<?php echo $cat_details->getUrl(); ?>" title="<?php echo $cat_details->getName(); ?>"> 
           <img src="<?php echo Mage::getBaseUrl('media', array('_secure' => true)) . 'catalog/category/' . $cat_details->getThumbnail(); ?>" alt="<?php echo $cat_details->getName(); ?>"/> 
          </a> 
         </div> 
         <div class="title"> 
          <a href="<?php echo $cat_details->getUrl(); ?>" title="<?php echo $cat_details->getName(); ?>"><?php echo $cat_details->getName(); ?></a> 
         </div> 
        </div> 
        <?php 
      } 
    } 
?> 
相关问题