如何使用Magento块显示多个类别

时间:2011-11-24 06:47:49

标签: magento

我的Magento主页目前有此代码段,可以很好地显示所有产品。

{{block type="catalog/product_list"  category_id="2" template="catalog/product/list.phtml"}}

粗略地说,我的类别树看起来与此类似

id 2 (root cat)
-> id 3
-> id 4
-> id 5

由于我添加的每个产品都是id 2的孩子 - 每个产品都显示在主页上。我所追求的是一个解决方案,允许我从主页产品列表中排除特定的ID(类别)。

我在下面尝试过这段代码但没有成功:

{{block type="catalog/product_list"  category_id="2,3,5" template="catalog/product/list.phtml"}}

1 个答案:

答案 0 :(得分:3)

您的代码{{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}}无效,因为块Mage_Catalog_Block_Product_List只加载一个类别$category = Mage::getModel('catalog/category')->load($this->getCategoryId());

我看到了两个针对您的问题的解决方案,您可以使用不同类别ID的块多次使用该块:

{{block type="catalog/product_list"  category_id="2" template="catalog/product/list.phtml"}}
{{block type="catalog/product_list"  category_id="3" template="catalog/product/list.phtml"}}
{{block type="catalog/product_list"  category_id="5" template="catalog/product/list.phtml"}}

或覆盖块Mage_Catalog_Block_Product_List并更改此部分的行为

        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        $this->_productCollection = $layer->getProductCollection();

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }