开发者

How do I get categories for a product in Magento

开发者 https://www.devze.com 2023-02-11 04:41 出处:网络
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:

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 = Ma开发者_如何学Cge::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?


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


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


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


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


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.


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*/  
0

精彩评论

暂无评论...
验证码 换一张
取 消