Magento: Get all products from a category
Here is a quick code to get all products from a particular/specified category in Magento.
First of all, you need to load the category (whose products you want to display) and get it’s ID. You can load a category by it’s ID or if you are in a category page then you can get category information from Magento registry. I have included code to load category in both ways.
You can fetch all enabled or disabled and visible or not visible products. We create ‘not visible’ products while creating configurable type products. In the below code, I have created a filter to fetch only enabled and visible products. You can comment out those filters to fetch disabled and not visible products as well.
Here is the full code:
<?php /** * If you want to display products from current category */ $category = Mage::registry('current_category'); /** * If you want to display products from any specific category */ $categoryId = 10; $category = Mage::getModel('catalog/category')->load($categoryId); /** * Getting product collection for a particular category */ $prodCollection = Mage::getResourceModel('catalog/product_collection') ->addCategoryFilter($category) ->addAttributeToSelect('*'); /** * Applying status and visibility filter to the product collection * i.e. only fetching visible and enabled products */ Mage::getSingleton('catalog/product_status') ->addVisibleFilterToCollection($prodCollection); Mage::getSingleton('catalog/product_visibility') ->addVisibleInCatalogFilterToCollection($prodCollection); /** * Printing category and products name */ echo '<strong>'.$category->getName().'</strong><br />'; foreach ($prodCollection as $val) { echo $val->getName() . '<br />'; } ?>Hope this helps. Thanks.
Pritesh
Pritesh Pethani