如何在Magento中获取产品的类别 [英] How do I get categories for a product in Magento

查看:78
本文介绍了如何在Magento中获取产品的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据产品的类别将product_type添加到我的Magento Google Base输出中,但是我似乎无法这样做.我有以下代码:

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

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

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

推荐答案

使用Rao在上面放置的源链接,我实际上找到了一个更好的答案:

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

这篇关于如何在Magento中获取产品的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆