1

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;

enter image description here

Why are they shown below again? I only want the subcats under the topcategories, not subcats twice.

1 Answer 1

2

Try changing your category collection like below-

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){
              if(in_array($child->getId(), $product->getCategoryIds())){   // change $product->getCategoryIds() to your product category id array
                 $out .="<li><a href='".$child->getUrl()."'>".$child->getName()."</a></li>";
              }
          }
          $out .="</ul>";
      $out .= "</li>";
  }

Update to get parent level categories only change your category collection to below code

$categoryCollection = Mage::getResourceModel('catalog/category_collection')
 ->addAttributeToSelect('name')
 ->addAttributeToFilter('level', 2)//2 is actually the first level
 ->addAttributeToSelect('url')
 ->addAttributeToFilter('entity_id', $currentCatIds)
 ->addIsActiveFilter();
9
  • I get a Fatal error: Call to a member function getCategoryIds() on null in on line 156 which is if(in_array($child->getId(), $product->getCategoryIds())){ // change $product->getCategoryIds() to your product category id array
    – twan
    Sep 29, 2017 at 13:09
  • please change $product->getCategoryIds() to variable of array of product category ids, may be with $currentCatIds
    – Piyush
    Sep 29, 2017 at 13:11
  • Nevermind I got it to work (kinda) but now my subcats are loaded not only under the topcats, but they are shown as if they are topcats. I will add an image as an example
    – twan
    Sep 29, 2017 at 13:11
  • I added the image
    – twan
    Sep 29, 2017 at 13:13
  • 1
    check my updated answer
    – Piyush
    Sep 29, 2017 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.