Header Ad

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

Ability to get all the manufacurers or brands in Magento

We can use the following code to get the list of manufacturers in MAGENTO.

$product = Mage::getModel('catalog/product');

//Retriving the attribute values for the manufacturer id
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'manufacturer') // This can be changed to any attribute code
->load(false);

$attribute = $attributes->getFirstItem()->setEntity($product->getResource());

//This is an array of manufacturers
$manufacturers = $attribute->getSource()->getAllOptions(false);