Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Get a list of all categories as Mage_Catalog_Model_Category
 
mp_daniel
Jr. Member
 
Total Posts:  17
Joined:  2009-03-25
 

Hi,

I want to customize the category navigation on the home page.
The default blank template generate a <ul> list of the categories via the top.phtml file:

<?php //if(count($this->getStoreCategories())>1): ?>
    
<ul id="nav">
    
<?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php 
echo $this->drawItem($_category?>
    <?php 
endforeach ?>
    
</ul>
<?php //endif; ?>

I’ve added a custom module and a custom block called CatalogOverview, which should also provide getStoreCategories() and drawItem().
But instead of a <ul> list, the drawItem() function should generate a table with all category images + category name.

The Problem is that the $category->getImageUrl() returns an empty string.

Here is the complete code:

<?php

class Mpsoft_CatalogOverview_Block_Category_Overview extends Mage_Core_Block_Template
{

    
/**
     * Get catagories of current store
     *
     * @return Varien_Data_Tree_Node_Collection
     */
    
public function getStoreCategories()
    
{
        $helper 
Mage::helper('catalog/category');
        return 
$helper->getStoreCategories();
    
}
    
    
    
protected function _getCategoryInstance()
    
{
        
if (is_null($this->_categoryInstance)) {
            $this
->_categoryInstance Mage::getModel('catalog/category');
        
}
        
return $this->_categoryInstance;
    
}    
    
    
/**
     * Get url for category data
     *
     * @param Mage_Catalog_Model_Category $category
     * @return string
     */
    
public function getCategoryUrl($category)
    
{
        
if ($category instanceof Mage_Catalog_Model_Category{
            $url 
$category->getUrl();
        
else {
            $url 
$this->_getCategoryInstance()
                ->
setData($category->getData())
                ->
getUrl();
        
}

        
return $url;
    
}
    
    
    
public function getCategoryImageUrl($category)
    
{
        
if ($category instanceof Mage_Catalog_Model_Category{
            $url 
$category->getImageUrl();
        
else {
            $url 
$this->_getCategoryInstance()
                ->
setData($category->getData())
                ->
getImageUrl();
        
}
        
return $url;
    
}
    
    
/**
     * Enter description here...
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int $level
     * @param boolean $last
     * @return string
     */
    
public function drawItem($category$level=0$last=false)
    
{
        $html 
'';
        
$html.= '<a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
        
        
$_imgUrl $this->getCategoryImageUrl($category);
        
$imgHtml '<img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($category->getName()).'" title="'.$this->htmlEscape($category->getName()).'" class="category-image" />';
        
        return 
$html.$imgHtml;
    
}    

    
public function getMyName()
    
{
        
return "test";
    
}
}
?>

I think the problem is that I don’t get a Mage_Catalog_Model_Category object which provides the getImageUrl() function.

Can anyone tell me the correct way to get the category image?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mkII
Sr. Member
 
Total Posts:  79
Joined:  2009-02-24
 

Hi mp_daniel,

Did you find out the way?
I want to obtain not only category’s image but also a category description, but I’m not a programmer, so I started to search, found your post and figured out you have no working solution right now. Is it still true?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mkII
Sr. Member
 
Total Posts:  79
Joined:  2009-02-24
 

Hi again.

Just look here: http://www.magentocommerce.com/boards/viewthread/14527/P0/
and you will find working receipts how to get a category image, description, how to add secondary image to category, etc.
Hope this helps.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top