我正在尝试根据产品的类别将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' );
}
Run Code Online (Sandbox Code Playgroud)
问题是它返回所有类别,而不仅仅是产品所在的类别.任何人都有解决方案吗?
小智 60
使用上面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();
}
Run Code Online (Sandbox Code Playgroud)
Rao*_*Rao 46
这完全没有经过测试..
//load the product
$product = Mage::getModel('catalog/product')->load($productId);
//load the categories of this product
$categoryCollection = $product->getCategoryCollection();
Run Code Online (Sandbox Code Playgroud)
然后,您可以$categoryCollection
像通过数组一样循环.
想要指出@Rao的解决方案是最好的,如果你有一个产品对象来获取类别ID,因为它只进行一次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();
}
Run Code Online (Sandbox Code Playgroud)
数组(1,2,3)是你的类别.请注意,数组有整数,而不是字符串值,我发现SQL可能很挑剔.
还想指出一次拉动一个类别的解决方案是非常低效的,因为它为循环的每次迭代进行SQL调用,例如:
foreach(..){
Mage::getModel('catalog/category')->load($categoryId);
}
Run Code Online (Sandbox Code Playgroud)
获取产品的所有类别
<?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' );
}
?>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
91599 次 |
最近记录: |