Magento为每个产品找到最深的类别
问题描述:
在我的产品网格页面上,我希望为每个产品展示它所在的“最深”类别。Magento为每个产品找到最深的类别
这是我到目前为止基于其他一些类似问题的主题。
$res = Mage::getSingleton('core/resource');
$eav = Mage::getModel('eav/config');
$nameattr = $eav->getAttribute('catalog_category', 'name');
$nametable = $res->getTableName('catalog/category') . '_' . $nameattr->getBackendType();
$nameattrid = $nameattr->getAttributeId();
$collection
->joinTable('catalog/category_product',
'product_id=entity_id', array('single_category_id' => 'category_id'),
null,'left')
->joinTable('catalog_category_entity',
"entity_id=single_category_id", array('cat_level' => 'level','cat_path' => 'path'),
null,'left')
//->addAttributeToSort('entity_id', 'ASC')
//->addAttributeToSort('cat_level', 'ASC')
//->groupByAttribute('entity_id')
->joinTable($nametable,
"entity_id=single_category_id", array('single_category_name' => 'value'),
"attribute_id=$nameattrid", 'left')
->printLogQuery(true);exit;
这使我得到以下结果:
所以我得到了我的产品收集并添加三列。显然,对于entity_id 310'Fauteuils'是最深的类别。我现在唯一要做的就是通过entity_id根据最高的cat_level对结果进行“分组”。然而,'group by'不会给我想要的结果。
感谢
答
这里有一个简单的解决方案,你可以采取的想法来自:
$category = $product->getCategory();
$path = explode('/', $category->getPath());
$deepestId = $path[count($path) - 1];
$deepestCategory = Mage::getModel('catalog/category')->load($deepestId);
Zend_Debug::dump($deepestCategory->getData());exit;
你会想看看在分类表中的列path
,然后找到最后一类ID在路径中。
谢谢,但我没有问题得到路径或类别级别,因为你可以看到我目前的进度(和截图)。另外,加载每个类别并不是非常聪明的性能。 – 2012-04-04 19:56:51
它完全为我工作。这是一个非常好的解决方案。 – RPDeshaies 2014-03-20 13:57:14