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の結果のスクリーンショット

だから私は私の製品コレクションを手に入れ、3つの列が追加されました。明らかに、entity_id 310の場合、「Fauteuils」が最も深いカテゴリです。私が今しなければならない唯一のことは、最高のcat_levelに基づいてentity_idによって結果を「グループ化」することです。ただし、「groupby」では目的の結果が得られません。

ありがとう

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 に答える