Magento: Get product category array from product iD

Magento: Get product category array from product iD

July 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Get product category array from product iD

Magento: Retrieve Product Categories Array from Product ID

In this tutorial, we will walk you through how to fetch product categories array from a specific product ID in your Magento store. This is particularly useful when you want to display the category hierarchy for individual products on different sections of your website.

Prerequisites

Before proceeding, ensure that you have:

  • A basic understanding of PHP and Magento’s core structure.
  • Access to your Magento admin backend with appropriate permissions.

Step 1: Get Product ID

To get the product ID, you can use the $product object in your controller or helper. Here’s an example:

$product = Mage::getModel('catalog/product')->load(123);

In this code snippet, replace 123 with the actual product ID you want to fetch.

Step 2: Get Categories Array from Product ID

Now that we have the $product object loaded with the desired product, let’s retrieve its categories. We’ll use the getCategoryIds() method provided by Magento:

$category_ids = $product->getCategoryIds();

The $category_ids variable will now contain an array of category IDs associated with the specified product.

Step 3: Get Child Category IDs

To fetch only the child categories (excluding parent categories like BRAND and CATEGORY), we need to filter the category IDs. Here’s how you can achieve this:

$child_category_ids = array_filter($category_ids, function ($id) {
    $category = Mage::getModel('catalog/category')->load($id);
    return $category->getLevel() > 2; // Change '2' to the desired level for child categories
});

In this code:

  • We use array_filter() to iterate over each category ID.
  • For each ID, we load the corresponding category object using Mage::getModel('catalog/category')->load($id).
  • The condition $category->getLevel() > 2 ensures that only categories with a level greater than ‘2’ are included in the final array. Adjust this value based on your specific category hierarchy.

Step 4: Echo Variables Where Needed

Now you can easily display the child category names wherever required using the following code:

foreach ($child_category_ids as $category_id) {
    $category = Mage::getModel('catalog/category')->load($category_id);
    echo $category->getName() . '<br>'; // Echo each category name with a line break
}

Conclusion

In this tutorial, we demonstrated how to retrieve product categories array from a specific product ID in Magento. By following the steps outlined above, you can efficiently fetch and display the child category hierarchy for individual products across different sections of your website.

Remember to adjust the category level condition in Step 3 according to your store’s specific category structure for accurate results.

For more advanced customizations or if you encounter any issues during implementation, feel free to explore our Magento development services at LaravelCompany. Our team of experienced Magento developers can assist with complex integrations and provide expert guidance tailored to your project requirements.

Catalog Security Admin

Our network