Monday, 16 March 2015

How to list all parent categories in magento



I'm trying to get all parent categories except root category from the collections returned using Mage::getModel('catalog/category');
My code :
$_category = Mage::getModel('catalog/category');
$_categories = $_category
->getCollection()
->addAttributeToSelect(array('name', 'image', 'description'));

foreach ($_categories as $_category){
print_r($_category);
die(0);
echo $_category->getName().'<>';
}


Example :
<?php
Mage::getModel('catalog/category');
$_helper = Mage::helper('catalog/category');


$_category = Mage::getModel('catalog/category');
$_categories = $_category
->getCollection()
->addAttributeToSelect(array('name', 'image', 'description','custom_attribute','thumbnail'));


foreach ($_categories as $_category){
if($_category->getcustom_attribute() == 'yes'){
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($_category);
$child= Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();
$imageSrc = Mage::getModel('catalog/category')->load($child)->getThumbnail();
$categoryModel = Mage::getModel('catalog/category')->load($_category->getId());
// Replace with Id of Cat
$productColl = Mage::getModel('catalog/product')->getCollection()
->addCategoryFilter($categoryModel)
->addAttributeToSort('price', 'asc')
->setPageSize(1)
->load();
$lowestProductPrice = $productColl->getFirstItem()->getPrice();
$img = $_imgUrl = Mage::getBaseUrl('media').'catalog/category/'.$imageSrc;
$url = $_helper->getCategoryUrl($_category);
$name = $_category->getName();
}
}

  ?>

No comments:

Post a Comment