php 如何在 Magento 中获取产品的类别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5009861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get categories for a product in Magento
提问by Rao
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:
我正在尝试根据产品的类别将 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' );
}
The issue is that it returns allof the categories, not just the ones the product is in. Anyone have a solution?
问题是它返回所有类别,而不仅仅是产品所在的类别。有人有解决方案吗?
回答by
Using the source link dropped by Rao above I actually found a better answer:
使用上面 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();
}
回答by Rao
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.
然后您可以$categoryCollection
像遍历数组一样循环。
回答by augsteyer
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.
想指出@Rao 的解决方案是最好的,如果您有一个产品对象来获取类别 ID,因为它只进行一次 SQL 调用。
If you just have category id's, you can do the following:
如果您只有类别 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();
}
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.
其中 array(1,2,3) 是您的类别。请注意,数组具有整数,而不是字符串值,我发现 SQL 可能对此很挑剔。
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.:
还想指出,一次提取一个类别的解决方案效率非常低,因为它对循环的每次迭代都进行 SQL 调用,例如:
foreach(..){
Mage::getModel('catalog/category')->load($categoryId);
}
回答by ravi patel
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' );
}
?>
回答by shampoo
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:
Rao 的解决方案尝试加载所有属性,这意味着大量查询和连接,但您只需要product_id
加载它的类别。所以你可以这样做:
$product = Mage::getModel("catalog/product")->setId($productId);
$categories = $product->getCategoryCollection();
This will not load the product and will give you the categories.
这不会加载产品,而是为您提供类别。
回答by swapnil Nandanwar
This code work in phtml file in Magento 2
此代码在 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*/