2012-04-04 4 views
4

私の製品のグリッドページには、各製品の「最も深い」カテゴリが表示されます。Magentoは各製品の最も深いカテゴリを見つけます

これは、私がこれまで出てきたもので、同様の質問に関する他のいくつかのトピックに基づいています。だから私は私の物の収集と3つの列が追加されました

Screenshot MYSQL results

:私は次のような結果になります

$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」は最も深いカテゴリーです。私が今しなければならないことは、最も高いcat_levelのentity_idベースで結果を「グループ化」することだけです。しかし、 '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を見つけることができます:ここで

答えて

4

は、あなたからのアイデアを取ることができます簡単な解決策がある

感謝パスで。

+0

ありがとうございますが、これまでの進捗状況(およびスクリーンショット)でわかるように、パスやカテゴリのレベルを取得するのに問題はありません。また、各カテゴリを読み込むことは、スマートなパフォーマンスには賢明ではありません。 –

+0

それは完全に私のために働いた。これは非常に素晴らしい解決策です。 – RPDeshaies

関連する問題