skip to Main Content

I want to show a list of categories a product belongs to, top categories and subcategories.

I figured out how to load the correct topcategories, but when I loop the subcats (children categories) it loads ALL subcats of that topcategory, not the categories that the product falls under.

Example:

enter image description here

Like you can see it loads a ton of subcategories, but only the ones with the red stripe are subcategories that the product falls under.

How can I make sure it only show those under their topcategories?

My code:

$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

Answers


  1. In your loop check if category has product or not. if($cat->getProductCount()){..} and same thing for $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;
    
    Login or Signup to reply.
  2. Please try my code, It will work.

    <?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
                }
        }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search