Magento Fetch category products to custom page
sometimes with Magento Fetch category products to custom page / custom block .. so here is simple code we can use and can have category’s products with ease
<?php $categoryId = (int category number); $category = Mage::getModel('catalog/category')->load($categoryId); $productcollection = 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($productcollection); Mage::getSingleton('catalog/product_visibility') ->addVisibleInCatalogFilterToCollection($productcollection); /** * Printing category and products name */ echo '<h1><strong>'.$category->getName().'</strong></h1>'; foreach ($productcollection as $val) { echo $val->getName() . '<br />'; } ?>
Here you may have noticed one code that filter product based on its availability or visibility
<?php Mage::getSingleton('catalog/product_status') ->addVisibleFilterToCollection($productcollection); Mage::getSingleton('catalog/product_visibility') ->addVisibleInCatalogFilterToCollection($productcollection); ?>