Magento Product’s Relative Parent Category Method

Today I want to quickly cover an area that I get asked about at least once a month, and that is “If a product belongs to more than one category.. how do I get the correct parent category while viewing a product through one category or the other?”.

With that said the, the biggest scenario for this need would be to determine the products parent category in order to perform something unique to the product’s page while viewing product’s page through one category or another.

This is a very simple method that assumes you have the product of question loaded up or rather, you are at the product level of your store in order to obtain some information about the products parent categories.

So here we go.

First you need to gather all the category id’s this product belongs to, so to do this we simply make a new variable called “$catIds” and gather all the category id’s like so:

getCategoryIds();
?>

Next we simply need to point to the first category in this new “$catIds” variable array like so:

(Remembere the number 0 is = 1 or rather first in an array.)

getCategoryIds();
$relativeCatId = $catIds[0];
?>

So now that we have gathered our relative parent category id, we can begin to load this category up in order to gather information from it and ultimately create a unique condition for our products page:

getCategoryIds();
$relativeCatId  = $catIds[0];
$category = Mage::getModel("catalog/category")->load($relativeCatId);
?>

Now that we have loaded up our relative parent category into this new variable called “$category” we can then say, “Grab the category’s name, or even attributes to create some kind of condition.”, like so:

getCategoryIds();
$relativeCatId   = $catIds[0];
$category =  Mage::getModel("catalog/category")->load($relativeCatId);
?>

getName()  == "Category One"): ?>
//DO THIS
getName()  == "Category Two"):?>
//DO THAT

And that’s it!

So to recap, all we did was while under a product level, gathered all its parent category id’s into the variable “$catIds”. Once all category id’s we captured, we proceeded to highlight or rather grabbed our first category’s id from the array (0) and used it to load the category up. Once this category was loaded, we could access its attributes or information. We used the category’s information to build a condition and perform something unique to the product page depending on if its being viewed through one category or a completely different category.

Thanks Everyone!

Devin R. Olsen

Devin R. Olsen

Located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

More Posts

Follow Me:
TwitterFacebookGoogle Plus

8 Responses to “Magento Product’s Relative Parent Category Method”

  1. Kim Bryant says:

    Hi Devin, thanks for sharing! I modified the code a bit so I could fetch the trail of categories I needed (between 3 and 6 levels of nested categories)

    Here’s my code, for anyone else who could use it:

    $category_ids = $_product->getCategoryIds();
    $number_categories = count($category_ids);

    switch ($number_categories)
    {
    case 3:
    $level3_category = Mage::getModel(“catalog/category”)->load($category_ids[2]);
    $category = $level3_category->getName();
    break;
    case 4:
    $level3_category = Mage::getModel(“catalog/category”)->load($category_ids[2]);
    $level4_category = Mage::getModel(“catalog/category”)->load($category_ids[3]);
    $category = $level3_category->getName() .’ > ‘. $level4_category->getName();
    break;
    case 5:
    $level3_category = Mage::getModel(“catalog/category”)->load($category_ids[2]);
    $level4_category = Mage::getModel(“catalog/category”)->load($category_ids[3]);
    $level5_category = Mage::getModel(“catalog/category”)->load($category_ids[4]);
    $category = $level3_category->getName() .’ > ‘. $level4_category->getName() .’ > ‘. $level5_category->getName();
    break;
    case 6:
    $level3_category = Mage::getModel(“catalog/category”)->load($category_ids[2]);
    $level4_category = Mage::getModel(“catalog/category”)->load($category_ids[3]);
    $level5_category = Mage::getModel(“catalog/category”)->load($category_ids[4]);
    $level6_category = Mage::getModel(“catalog/category”)->load($category_ids[5]);
    $category = $level3_category->getName() .’ > ‘. $level4_category->getName() .’ > ‘. $level5_category->getName() .’ > ‘. $level6_category->getName();
    break;
    }

  2. Prabhjot Singh says:

    Thank you very much. Was looking for this since days. Thanks a lot. I am new to Magento and this really helped.

  3. This could be tricky, because one product can be assigned to different categories with different level of nesting.

  4. Darren Fauth says:

    Devin-

    Thanks for this simple and straight forward post. Working on my second Magento site and it is making a lot more sense than the first one. Finding gems like this really helps to add to my arsenal of knowledge and knowing the sky’s the limit on customizing product pages for certain categories.

  5. President Hamid Karzai's administration is corrupt says:

    Aw, this was a really quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I procrastinate a lot and never seem to get something done.

  6. I always enjoy feedback from my readers!

Leave a Reply