【问题标题】:How to categorize Magento Grouped Product Page?如何对 Magento 分组产品页面进行分类?
【发布时间】:2012-10-26 09:39:38
【问题描述】:

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

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

例如:- 我有两个类别 餐桌 和 餐椅。

我创建了一个分组产品,例如 Lyn Dining set,并关联了两个简单的产品 lyn Dining table(来自 Dining Tables 类别)和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();
            }

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

【问题讨论】:

    标签: magento e-commerce categories magento-1.7 product


    【解决方案1】:

    我会创建一个辅助函数,像这样(请记住,这段代码还是有点粗糙):

    公共函数 formatProductsWithCategories($products) { $categories = 数组(); $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; ?>
    

    【讨论】:

      猜你喜欢
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多