4

在我的产品网格页面上,我想为每个产品显示它所在的“最深”类别。

这是我迄今为止提出的,基于关于类似问题的其他一些主题。

    $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;

这给了我以下结果:

截图 MYSQL 结果

所以我得到了我的产品集合并添加了三列。显然,对于 entity_id 310,“Fauteuils”是最深的类别。我现在唯一要做的就是根据实体 ID 根据最高的 cat_level 对结果进行“分组”。但是,“分组依据”并没有给我想要的结果。

谢谢

4

1 回答 1

4

这是一个简单的解决方案,您可以从中获取想法:

$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-04T13:57:23.847 回答