在我的产品网格页面上,我想为每个产品显示它所在的“最深”类别。
这是我迄今为止提出的,基于关于类似问题的其他一些主题。
$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”是最深的类别。我现在唯一要做的就是根据实体 ID 根据最高的 cat_level 对结果进行“分组”。但是,“分组依据”并没有给我想要的结果。
谢谢