If you enable the flat categories in your Magento store, some code that gets children of a given category might stop working. The following code can be used to get all the cildren of a given category with enabled flat…
Getting products from a category (Magento 1)
The following code returns all products from within a certain category in a collection.
1 2 3 |
// category id = 5 // addAttributeToSelect is optional $products = Mage::getModel('catalog/category')->load(5)->getProductCollection()->addAttributeToSelect('*'); |
Getting the parent category of a category (Magento 1)
Use the following code to get a Magento categories parent category.
1 |
Mage::getSingleton('catalog/layer')->getCurrentCategory()->getParentCategory(); |
Getting the name of the current category (Magento 1)
While being on the Magento category page, it might be useful to display the category name. Use the following code to do so.
1 |
Mage::getSingleton('catalog/layer')->getCurrentCategory()->getName(); |
Getting a category URL using the category id (Magento 1)
To get a categories URL by category id, use the following code:
1 2 |
$categoryId = 55; $url = Mage::getModel("catalog/category")->load($categoryId)->getUrl(); |