2

Magento 1.7 でグループ化された製品ページを作成する必要があります

そのために、グループ化された製品として製品を追加し、そこにいくつかの単純な製品を関連付けました。グループ化された製品は、表の行に 1 つずつフロント エンドに表示されます。私の要件は、関連製品の対応するカテゴリでこのページを表示することです。

例: -ダイニング テーブルダイニング チェアの 2 つのカテゴリがあります。

Lyn Dining setというグループ化された製品を作成し、2 つの単純な製品lyn ダイニング テーブル(ダイニング テーブルカテゴリから) とLyn ダイニング チェア(ダイニング チェアカテゴリから) を関連付けました。デフォルトでは、表の行に 1 つずつ表示されます。このような:-

Lyn Dining Table    Product Attributes...
Lyn Dining Chair    Product Attributes...
......................................
......................................
......................................

しかし、対応するカテゴリヘッダーに表示する必要があります。あれは:-

Dining Table:-
Lyn Dining Table    Product Attributes...
......................................
......................................

Dining Chair:-
Lyn Dining Chair    Product Attributes...
......................................
......................................

そのために、grouped.phtml ファイルを編集しました。まず、このコードでカテゴリ名を取得しました:-

        /**
         * get categories from a product
         */
        $categoryIds = $this->htmlEscape($_item->getCategoryIds());

        /**
         * looping through the array of category ids
         */
        foreach($categoryIds as $categoryId) {
            $category = Mage::getModel('catalog/category')->load($categoryId);
            $categoryB=$category->getName();
            }

これにより、カテゴリ名を取得できます。しかし、すべての製品を対応するカテゴリにリストできるようにループを実装する方法がわかりません。どなたか手を貸してください。

4

1 に答える 1

0

次のようなヘルパー関数を作成します (このコードはまだ少し荒いので注意してください)。

public function formatProductsWithCategories($products) { $categories = array(); $categoryProducts = 配列(); $hasOrphans = false;

    foreach ($products as $product) {
        $categories = array_merge($product->getCategoryIds(), $categories);
        foreach($product->getCategoryIds() as $categoryId) {
            $categoryProducts[$categoryId][] = $product;
            $found = true;
        }

        if (!$found){
            $categoryProducts["empty"][] = $product;
            $hasOrphans = true;
        }
    }

    $categoryCollection = Mage::getResourceModel('catalog/category_collection')
        ->addAttributeToFilter('entity_id', array('in', $categories))
        ->addAttributeToSelect('name');

    $categoryCollection->load();

    if ($hasOrphans) {
        $categoryCollection->addItem(
            Mage::getModel('catalog/category')
                ->addData(array(
                    'id' => 'empty',
                    'name' => 'No Categories'
            ))
        ); // assigning orphan values
    }

    foreach ($categoryCollection as $category) {
        if (array_key_exists($category->getId(), $categoryProducts)) {
            $category->setProducts($categoryProducts[$category->getId()]);
        }
    }

    return $categoryCollection;
}

次に、grouped.phtml テンプレートで次のようにします。

<?php $categories = Mage::helper('helper_module/category')->formatProductsWithCategories($_associatedProducts); ?>
<?php foreach ($categories as $category):?>
    <tr><td><?php echo $category->getName();?></td></tr>
    <?php foreach ($category->getProducts() as $_item): ?>
         // . . .  Existing loop here . . .
    <?php endforeach; ?>
<?php endforeach; ?>
于 2012-11-16T18:07:16.877 に答える