如何仅加载属于当前产品的子类别(magento)

时间:2017-09-29 12:56:10

标签: php loops magento magento-1.9

我想显示产品所属的类别列表,顶级类别和子类别。

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

示例:

enter image description here

就像你可以看到它加载了大量的子类别,但只有红色条纹的子类别是产品所属的子类别。

我怎样才能确保它只显示在其顶级类别下的那些?

我的代码:

$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;

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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
            }
    }
?>