如何对Magento分组产品页面进行分类?

如何对Magento分组产品页面进行分类?,magento,e-commerce,categories,magento-1.7,product,Magento,E Commerce,Categories,Magento 1.7,Product,我需要在Magento 1.7上创建一个分组产品页面 为此,我添加了一个产品作为分组产品,并在那里关联了一些简单的产品。 分组后的产品在前端以表格行的形式逐个显示。我的要求是在相关产品的相应类别中显示此页面 我有两类餐桌和餐椅 我已经创建了一个分组产品,比如说Lyn餐饮套装,并关联了两个简单产品Lyn餐桌(来自餐桌类别)和Lyn餐椅(来自餐椅类别)。默认情况下,它将在表格行中逐个显示。像这样:- Lyn Dining Table Product Attributes... Lyn Dini

我需要在Magento 1.7上创建一个分组产品页面

为此,我添加了一个产品作为分组产品,并在那里关联了一些简单的产品。 分组后的产品在前端以表格行的形式逐个显示。我的要求是在相关产品的相应类别中显示此页面

我有两类餐桌和餐椅

我已经创建了一个分组产品,比如说Lyn餐饮套装,并关联了两个简单产品Lyn餐桌(来自餐桌类别)和Lyn餐椅(来自餐椅类别)。默认情况下,它将在表格行中逐个显示。像这样:-

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();
            }

通过这个,我可以获取类别名称。但我不知道如何实现循环,以便所有产品都可以在相应的类别中列出。请大家帮忙。

我会创建一个助手函数,如下所示(请记住,此代码仍然有点粗糙):

公共函数格式ProductsWithCategories($products) { $categories=array(); $categoryProducts=array(); $HASOLVES=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; ?>

//…此处存在循环。