Magento 1.9 – How to Retrieve Category Tree for Individual Products

magento-1.9PHP

I am trying to create an export of a products sku and its category tree. Obviously getting the category Id's is simple enough but I need to turn that array of Id's into text category paths.

ie: lets say that product with sku "xyz123" is in "root/plumbing/pipes" and is also in "root/someother/accessories", currently I can retrieve the array of Id's but what is a good way to reconstruct each of those text trees?

EDIT: Using the example that was posted below, I created a class from the modified example to create what I was looking for.

class categories 
{
  protected $_index = array();

  public function __construct() {
      $root = Mage::app() -> getStore() -> getRootCategoryId();
      $this -> _initCategoryTree($root, false);
  }

  public function getCategory($id) {
      return $this -> _index[$id] ? : false;
  }

  public function toHtml() {
      $index = $this -> _getIndex();
      $html = "<table border><tr><th>line</th><th>key</th><th>value</th></tr>";
      $iter = 0;
      foreach ($index as $k => $v) {
          $html .= "<tr>";
          $html .= "<td>" . $iter++ . "</td>";
          $html .= "<td>" . $k . "</td>";
          $html .= "<td>" . $v . "</td>";
          $html .= "</tr>";
      }
      $html .= "</table>";
      echo $html;
  }

  private function _initCategoryTree($rootId, $isChild, $parentName = null) {
      //@formatter:off
      $collection = Mage::getModel('catalog/category') 
          -> getCollection() 
          -> addAttributeToSelect('*') 
          -> addAttributeToFilter('parent_id', array('eq' => $rootId));

      //@formatter:on
      $index = array();

      foreach ($collection as $category) {
          $name = $category -> getName();
          $id = $category -> getId();

          $index[$id] = $name;

          $children = $category -> getChildren();
          $subIndex = array();

          if ($children != '') {
              $subIndex = $this -> _initCategoryTree($id, true, $name);
              foreach ($subIndex as $key => $child) {
                  $this -> _index[$key] = $child;
                  $this -> _index[$key] = $name . DS . $this -> _index[$key];
                  if (null !== $parentName)
                      $this -> _index[$key] = $parentName . DS . $this -> _index[$key];
              }
          }

      }

      if (!$isChild) {
          foreach ($index as $key => $category) {
              $this -> _index[$key] = $category;
          }
      }

      return $index;
  }

  private function _getIndex() {
      if (null === $this -> _index)
          return false;
      return $this -> _index;
  }

  private function _dump($value) {
      echo "<pre>";
      ob_start();
      var_dump($value);
      echo ob_get_clean();
      echo "</pre>";
  }

}

$cTree = new categories();
//toHtml is just a quick method to dump the index as an html table for debug purposes.
$cTree -> toHtml();

Best Answer

You'll want to get the category ids from the product, then loop through them and use logic similar to the function here: Category tree from specific parent category

Related Topic