Header Ad

Showing posts with label Category in Magento. Show all posts
Showing posts with label Category in Magento. Show all posts

Thursday, April 1, 2010

How to get sub category id's under a category in Magento

The following code will provide the necessary steps for fetching the sub-category id's under selected category.

//this will return the category object
$_category = Mage::getModel('catalog/category')->load($categoryId);

$subcategoryIds = array();

//This will return the list of category id's under specified category
$subcategoryIds = split(",", $_category->getChildren());

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