tjw
tjw

Reputation: 51

How to display multiple categories with a Magento block

My Magento homepage currently has this code snippet that displays all products just fine.

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

Roughly, my category tree looks similar to this

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

Since every product I add is a child of id 2 - every product shows up on the homepage. What I'm after is a solution that will allow me to exclude specific id's (categories) from the home page product list.

I've tried this snippet below with no success:

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

Upvotes: 1

Views: 8368

Answers (1)

Sylvain Rayé
Sylvain Rayé

Reputation: 2466

Your code {{block type="catalog/product_list" category_id="2,3,5" template="catalog/product/list.phtml"}} won't work because the block Mage_Catalog_Block_Product_List load only one category $category = Mage::getModel('catalog/category')->load($this->getCategoryId());.

I see two solutions for your problem, you can use the block more than once with a different category 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"}}

Or overwrite the block Mage_Catalog_Block_Product_List and change the behavior of this part

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

Upvotes: 3

Related Questions