I want to create a page in Magento that shows a visual representation of the categories.. example
CATEGORY
product 1
product 2
ANOTHER CATEGORY
product 3
From code found in an SEO related class (Mage_Catalog_Block_Seo_Sitemap_Category)
$helper = Mage::helper('catalog/category');
$collection = $helper->getStoreCategories('name', true, false);
$array = $helper->getStoreCategories('name', false, false);
Try to forget that it's a database that's powering your store, and instead concentrate on using the objects that the Magento system provides.
For example, I had no no idea how to get a list of categories. However, I grepped through the Mage codebase with
grep -i -r -E 'class.+?category'
Which returned a list of around 30 classes. Scrolling through those, it was relatively easy to guess which objects might have methods or need to make method calls that would grab the categories.
I made this little video on how I create custom category listing blocks with Magento. I am sure there are better ways of achieving this or even something I could have done better, but it's just my method. I only created this it in hopes that it helps explain somethings to some people out there.
Magento Custom Category Listing Tutorial
Thanks a lot. Really helps. To get the game, make a loop and then getName()
foreach ($collection as $cat):
echo $cat->getName();
endforeach;
Here's a quick example
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load();
I used this in /app/design/frontend/default/default/template/catalog/product/feature.xml
<?php
/**
* Home page Featured Product list template
*
* @see Mage_Catalog_Block_Product_List
*/
?>
<?php
if (!is_null($this->_productCollection)) {
$_origCollection = $this->_productCollection;
$this->setCollection(null);
}
$this->setCategoryId(16);
$_productCollection=$this->getLoadedProductCollection() ?>
<?php if($_productCollection->count()): ?>
<div class="featured-products">
<h2><?php echo $this->__('Featured Products') ?></h2>
<?php foreach ($_productCollection as $_product): ?>
<div>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150, 50); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
<h3 class="product-name"><?php echo $this->htmlEscape($_product->getName())?></h3>
<?php echo nl2br($this->htmlEscape($_product->getShortDescription())) ?>
</a>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
category Listing block:
<?php
$categories = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
?>
<ul>
<?php foreach($catIds as $catId): ?>
<li>
<?php
$category = Mage::getModel('catalog/category')->load($catId);
echo $category->getName();
$subCats = Mage::getModel('catalog/category')->load($category->getId())->getChildren();
$subCatIds = explode(',',$subCats);
?>
<?php if(count($subCatIds) > 1):?>
<ul>
<?php foreach($subCatIds as $subCat) :?>
<li>
<?php
$subCategory = Mage::getModel('catalog/category')->load($subCat);
echo $subCategory->getName();
?>
</li>
<?php endforeach;?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>