How do I get categories for a product in Magento

后端 未结 6 2121
情书的邮戳
情书的邮戳 2021-01-31 16:12

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:

// Ge         


        
6条回答
  •  生来不讨喜
    2021-01-31 16:42

    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);
    }
    

提交回复
热议问题