如何在Magento中为产品获取分类

36

我想根据商品的类别将product_type添加到我的Magento Google Base输出中,但似乎无法实现。 我有以下代码:

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

问题在于它返回所有类别,而不仅仅是产品所在的类别。有人有解决方法吗?


这个问题更适合在 Stack Overflow 上提问。 - Christopher
6个回答

65

使用Rao上方提供的源链接,我实际上找到了更好的答案:

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

这算是正确答案,但从未被标记为正确。 - ShaunOReilly
我正在寻找相同的东西..但我应该把这个代码放在哪里..在哪个文件夹..你能给我路径吗?我想要类别列表以JSON格式。我该怎么办才能得到它? - Pavan Kumar
15
不要这样做,使用Rao的解决方案,这将节省大量的SQL查询! - Fabian Blechschmidt

49

这完全没有经过测试...

//load the product 

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

//load the categories of this product 

$categoryCollection = $product->getCategoryCollection();
你可以像遍历数组一样遍历$categoryCollection来源

1
这应该是正确的解决方案 - 通过使用集合,您可以使用连接而不是(至少)每个类别1个查询。(参见:Mage_Catalog_Model_Resource_Product::getCategoryCollection - Phil Birnie
1
getCategoryCollection() 不包含类别名称作为其选择的一部分。 - dchayka
3
虽然集合是可扩展的,但它可以做到。尝试使用 getCategoryCollection()->addAttributeToSelect('name') (注意:未经测试)。 - Doug McLean
@DougMcLean测试过并且可行。那个评论应该得到更多的赞扬。 - tim.baker
你好@Rao,这是我的代码类别ID没有打印->paste.ofcode.org/3aRT4NyTGRey9AeQ8xnrQtw感谢您的帮助? - Gem

7

想要指出,如果您有一个产品对象需要获取类别 ID,则 @Rao 的解决方案是最好的,因为它仅进行一次 SQL 调用。

如果您只有类别 id,可以这样做:

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

其中array(1,2,3)是您的类别。请注意,该数组具有整数值,而不是字符串值,我发现SQL可能对此很挑剔。

还要指出的是,每次迭代循环时,仅拉取一个类别的解决方案非常低效,因为它会对SQL进行一次调用,例如:

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

5
获取所有产品类别
<?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' );
   } 
?>

1

Rao的解决方案尝试加载所有属性,这意味着需要大量的查询和连接,但是你只需要一个product_id来加载它的类别。因此,你可以这样做:

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

这不会加载产品,而是给你分类。

0

这段代码适用于Magento 2中的phtml文件

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接