質問

設計のために、各製品の製品カテゴリパスを取得する必要があります。カタログ/製品/list.phtmlの次のコードを使用して、カテゴリパスを取得し、最初のレベルを爆発させることができました。

<?php $currentCat = Mage::registry('current_category'); ?>
<?php $exp = explode("/", $currentCat->getPath());?>

一方、私は、メインカテゴリに基づいて各製品の異なる色を表示するために、良好な結果がないため、各製品ディスプレイの各製品ディスプレイのカテゴリパスを検索結果で取得しようとしました。

どんな助けも感謝します。 BRGDS。

役に立ちましたか?

解決

次のコードは、ルートカテゴリ(ID:1)から始まる配列を提供し、次にルートカテゴリをストアで、次に製品のカテゴリまでのパスを提供します。

$category = Mage::getModel('catalog/product')->load([id])->getCategory();
$path = explode('/', $category->getPath());

しかし、これは、設計目的のためだけに非常に「重い」コードです。製品のコレクションを拡張する方が良いかもしれません Mage_Catalog_Block_Product_List クラス、メソッド _getProductCollection 参加 catalog_category_entity コレクションクエリと追加のテーブル path 選択フィールドへのフィールド

他のヒント

ルートカタログからパスを取得します

public function getCategoryPath($sku)
{
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);

    $pathArray = array();
    $collection1 = $product->getCategoryCollection()
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addAttributeToSelect('path')
        ->addAttributeToSelect('is_active');


    foreach($collection1 as $cat1){            
        $pathIds = explode('/', $cat1->getPath());            
        $collection = Mage::getModel('catalog/category')->getCollection()
            ->setStoreId(Mage::app()->getStore()->getId())
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_active')
            ->addFieldToFilter('entity_id', array('in' => $pathIds));

        $pahtByName = '';
        foreach($collection as $cat){                
            $pahtByName .= '/' . $cat->getName();
        }

        $pathArray[] = $pahtByName;

    }

    return $pathArray;
}

次のコードを使用して、サイトの外側の製品ページに着陸する際に、パン粉の製品カテゴリを見つけました。 (いつ if ($category = $this->getCategory()) 失敗)

$cate 最後のカテゴリ製品が見つかりました。カテゴリ関数を使用してパスを取得できます。

Magento/app/code/core/mage/catalog/helper/data.php

カテゴリ以外の場所から来る場合、製品が見つかった場所を追加します

修正が必要:TODO:ループする前に製品がカテゴリを持っているかどうかを確認してください!

if(empty($path)) {
    if ($this->getProduct()) {
        $collection =  Mage::getModel('catalog/category')->getCollection()
            ->setStoreId(Mage::app()->getStore()->getId())
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_active')
            ->addFieldToFilter('entity_id', array('in' => $this->getProduct()->getCategoryIds()));
        $categories = array();
        $cate = "";
        foreach ($collection as $category) {
            $cate = $category;
            $pathInStore = $category->getPathInStore();
            $pathIds = array_reverse(explode(',', $pathInStore));
        }
        $categories = $cate->getParentCategories();
        if($categories) {
        foreach($pathIds as $categoryId) {
                $path['category'.$categoryId] = array(
                    'label' => $categories[$categoryId]->getName(),
                    'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getURL() : ''
                );
        }}
    }
}

私がこれを達成するのに役立ったあなたの答えをありがとう。

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top