Home » Php » php – How do I get categories for a product in Magento

php – How do I get categories for a product in Magento

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to add product_type to my Magento Google Base output based on the product’s categories, but I seem to be unable to. I have the following code:

// Get categories from  product to include as product_type
$categoryIds = $object->getCategoryIds();
foreach($categoryIds as $categoryId) {
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $this->_setAttribute('product_type', $category->getName(), 'text' );
}

The issue is that it returns all of the categories, not just the ones the product is in. Anyone have a solution?

How to&Answers:

Using the source link dropped by Rao above I actually found a better answer:

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

$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
    $_cat = Mage::getModel('catalog/category')->load($category_id) ;
    echo $_cat->getName();
} 

Answer:

This is utterly not tested..

//load the product 

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

//load the categories of this product 

$categoryCollection = $product->getCategoryCollection();

You can then loop through $categoryCollection like through an array.

source

Answer:

Want to point out that the solution by @Rao is the best if you have a product object to get category ID’s from as it makes only one SQL call.

If you just have category id’s, you can do the following:

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('name') //you can add more attributes using this
    ->addAttributeToFilter('entity_id', array('in'=>array(1,2,3)));

foreach($categories as $_cat){
    $holder[]= $_cat->getName();
}

Where array(1,2,3) are your categories. Note that the array has integers, not string values, I found that SQL can be picky about that.

Also wanted to point out that solutions pulling one category at a time are very inefficient as it makes an SQL call for every iteration of the loop e.g.:

foreach(..){
    Mage::getModel('catalog/category')->load($categoryId);
}

Answer:

Get all Categories of the Product

<?php
    $_Product = Mage::getModel("catalog/product")->load( PRODUCT_ID );
    $categoryIds = $_Product->getCategoryIds();//array of product categories

    foreach($categoryIds as $categoryId) {
      $category = Mage::getModel('catalog/category')->load($categoryId);
      $this->_setAttribute('product_type', $category->getName(), 'text' );
   } 
?>

Answer:

Rao’s solution tries to load all the attributes which means lots of queries and joins but you only need a product_id to load it’s categories. So you can do this:

  $product = Mage::getModel("catalog/product")->setId($productId);
  $categories = $product->getCategoryCollection();

This will not load the product and will give you the categories.

Answer:

This code work in phtml file in Magento 2

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$categories = $product->getCategoryIds(); /*will return category ids array*/