Header Ad

Saturday, March 27, 2010

Ability to Get Products under a category in Magento

Hi,

We can use the following code to get the products under a category.



$_categories=$this->getCurrentChildCategories();

$_category = $this->getCurrentCategory();
$subs = $_category->getAllChildren(true);
$result = array();
foreach($subs as $cat_id) {
$category = new Mage_Catalog_Model_Category();
$category->load($cat_id);
$collection = $category->getProductCollection();
foreach ($collection as $product) {
$result[] = $product->getId();
}

}
shuffle($result);
?>

No comments: