Get all parent id of sub category


Consider this tree as example.

2
 - 5
   - 8
 - 6
   - 9
   - 12
     - 20

The easiest way to do so is this:

$category = Mage::getModel('catalog/category')->load(20);

Each category has a field called path that contains the ids of the parents and current category.

$path = $category->getPath();

This should return something like this 1/2/6/12. All you need to do is to split this string by / and remove the first element. That one is the id for ‘root of all roots’.

$ids = explode('/', $path);
unset($ids[0]);
Advertisements

2 comments on “Get all parent id of sub category

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s