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

Page 3 of 3
sub-categories displayed instead of products
 
robbo23
Sr. Member
 
Total Posts:  81
Joined:  2008-10-20
 

Has anyone got this working in v 1.2.0.1?

I tried the instructions given earlier but ended up with a parse error.

 
Magento Community Magento Community
Magento Community
Magento Community
 
moomarketing
Jr. Member
 
Total Posts:  10
Joined:  2009-04-03
 

TOP MARKS, this was driving me mental!!!

I got this working perfectly using dunks example on page on. Only thing to note is in version 1.3.1 do not update the function in navigation.php just copy dunk’s function and paste into view.php as per his example.

Thanks a million!!

For Magento - Why is this not a standard feature? The site I am building has 2000+ products in certain categories!

EDIT:

For anyone who then wants the sub cats to be laid out like the products would be and also make use of the standard magento no-image image use the following code for your catnav.phtml page:

<?
$cols
=3;
$i=0;
?>
<div class="cat-nav-table">    
    <
div class="listing-type-grid catalog-listing">
    
<?foreach ($this->getCurrentChildCategories() as $_childCategory):?>
        <?if
($i>&& $i%$cols==0):?>
            
</ol>
        
<?endif;?>
        <?if
($i%$cols==0):?>
            
<ol class="grid-row odd">
        
<?endif;?>
                
<li class="item">
                    <
div class="imageDiv">
                    <
a href="<?=$this->getCategoryUrl($_childCategory)?>"<?if ($this->isCategoryActive($_childCategory)):?> class="activeNav"<?endif?>>
                    
<?
                    
if($_childCategory->getImageUrl()){
                        $catImage 
$_childCategory->getImageUrl();
                    
}else{
                        $catImage 
$this->getSkinUrl('images/catalog/product/placeholder/small_image.jpg');
                    
}
                    ?>
                    
<img src="<?= $catImage ?>" alt="<?=$_childCategory->getName()?>" width="135" height="135"/>
                    <
p><?=$_childCategory->getName()?></p>
                    </
a>
                    </
div>
                </
li>
        
<? $i++ ?>
    <?endforeach
;?>
    
</div>
</
div>
 
Magento Community Magento Community
Magento Community
Magento Community
 
JMac
Jr. Member
 
Total Posts:  10
Joined:  2009-04-15
 

Ok i got this woking too, but i had to replace the function in Navigation.php - v1.3.2

Still quite amazed that this isnt built into Magento as standard. Most online stores have a category > Sub category structure.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rolomode
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2008-10-29
Mar del Plata
 

Has anyone got this working in v 1.3.2.3?

 
Magento Community Magento Community
Magento Community
Magento Community
 
kukidunk
Jr. Member
 
Total Posts:  9
Joined:  2009-08-13
Nottingham, England
 

To get this working in 1.3.2.3:

Follow Dunk’s excellent step-by-step from page 1, but don’t change anything in Navigation.php or View.php.

Instead edit the function getChildrenCategories($category) in app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Category.php to add the line ->addAttributeToSelect(’image’)

public function getChildrenCategories($category)
    
{
        $collection 
$category->getCollection();
        
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
$collection->addAttributeToSelect('url_key')
            ->
addAttributeToSelect('name')
            
//start of edit
            
->addAttributeToSelect('image')
            
//end of edit
            
->addAttributeToSelect('all_children')
            ->
addAttributeToSelect('is_anchor')
            ->
addAttributeToFilter('is_active'1)
            ->
addIdFilter($category->getChildren())
            ->
setOrder('position''ASC')
            ->
joinUrlRewrite()
            ->
load();
        return 
$collection;
    
}

I agree with others that this feature should indeed by part of Magento as standard.

A way of setting this up without editing core code would be nice, too.

 
Magento Community Magento Community
Magento Community
Magento Community
 
benz001
Sr. Member
 
Avatar
Total Posts:  238
Joined:  2009-02-05
Sydney
 

Slightly off topic - but very much related - I’ve been having problems with this approach in 1.3.2.3, it worked in one context (going through the getChildren route to show them in a category view) but it didn’t work when going attempting to use the sub-category images inside the navigation menu.

I ended up having to modify Mage/Catalog/Model/Resource/Eav/Mysql4/Category/Tree.php instead to include the image attribute.

protected function _getDefaultCollection($sorted=false)
    
{
        $this
->_joinUrlRewriteIntoCollection true;
        
$collection Mage::getModel('catalog/category')->getCollection();
        
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */

        
$collection->addAttributeToSelect('name')
            ->
addAttributeToSelect('url_key')
                
//Add Here
                
->addAttributeToSelect('image')
                
//End Additions
            
->addAttributeToSelect('is_active');

        if (
$sorted{
            
if (is_string($sorted)) {
                
// $sorted is supposed to be attribute name
                
$collection->addAttributeToSort($sorted);
            
else {
                $collection
->addAttributeToSort('name');
            
}
        }

        
return $collection;
     
}

Once I had done that I was then able to modify Mage/Catalog/Block/Navigation.php to add the image into the drawItem method.

public function drawItem($category$level=0$last=false)
    
{
        
...        
$html.= '">'."\n";
        
//Addition
        
if ($category instanceof Mage_Catalog_Model_Category{
            $url 
$category->getImageUrl();
        
else {
        
            $url 
$this->_getCategoryInstance()
                ->
setData($category->getData())
                ->
getImageUrl();
        
}

        $html
.= '<a href="'.$this->getCategoryUrl($category).'"><img src="'.$url.'"/><span>'.$this->htmlEscape($category->getName()).'</span></a>'."\n";
        ...       
    
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
benz001
Sr. Member
 
Avatar
Total Posts:  238
Joined:  2009-02-05
Sydney
 

PS all this was of course done in /app/code/local/ so that core modifications won’t overwrite it.
Also flat categories is turned off - I’ve had all sorts of grief with that turned on.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ruben_diez
Jr. Member
 
Total Posts:  6
Joined:  2010-03-25
 

I use Magento ver. 1.4.0.1 excaim

This function for 1.4.0.1:

app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Category.php

And I have copied to:

app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Category.php

line 572 aprox

the same thing…

public function getChildrenCategories($category)
    
{
        $collection 
$category->getCollection();
        
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
$collection->addAttributeToSelect('url_key')
            ->
addAttributeToSelect('name')
            
// start of edit
            
->addAttributeToSelect('image')
            
// end of edit
            
->addAttributeToSelect('all_children')
            ->
addAttributeToSelect('is_anchor')
            ->
addAttributeToFilter('is_active'1)
            ->
addIdFilter($category->getChildren())
            ->
setOrder('position''ASC')
            ->
joinUrlRewrite()
            ->
load();
        return 
$collection;
    
}

thanks for the help again. I don’t know why is all so complicated…

 
Magento Community Magento Community
Magento Community
Magento Community
 
ubedarab
Jr. Member
 
Total Posts:  10
Joined:  2010-04-28
 

let take backup your oringinal view.phtml file in
app\\design\\frontend\\yourdiractory_path\\default\\template\\catalog\\category

copy this code to view.phtml file
u get subcategory on your category page

i hope this code is useful for u ppl

enjoy with this code
grin

File Attachments
view.phtml  (File Size: 3KB - Downloads: 269)
 
Magento Community Magento Community
Magento Community
Magento Community
 
unfeasible
Sr. Member
 
Avatar
Total Posts:  94
Joined:  2008-03-03
New Zealand
 

Hi, this is what is in the above post, except with DIV’s.

.main-container .category-custom-products .products-grid .item { width:160px; float:left; padding:5px; border:1px solid #ccc; margin:4px; }

<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);

$rootcat = $_category->entity_id;
$cat = Mage::getModel('catalog/category')->load($rootcat);
$child = $cat->getChildren();
//echo $child;

if($child)
{
//echo "<pre>";
//print_r($_category);
//echo "</pre>";
//die;
$helper = Mage::helper('catalog/category');
$_helper = $this->helper('catalog/output');
?>
<div class="category-custom-products">
<span class="page-title">
<?php echo $_helper->categoryAttribute($_category, $_category->getName(), 'name') ?></span>

<div class="products-grid">

<?foreach ($collection as $cat):?>
<?php if($_category->getIsActive()):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
//echo $cat->getId();
$_img = $cur_category->getImageUrl();
?>

<div class="item">
<a href="<?php echo $helper->getCategoryUrl($cat);?>"><img src="<?php echo $_img?>” /></a>
<a href="<?php echo $helper->getCategoryUrl($cat);?>"><?php echo $cat->getName();?></a>
</div>

<?php endif?>
<?php endforeach;?>

</div></div>
<?php
}
else
{
?>
<?php echo $this->getProductListHtml() ?>
<?php
}
?>

File Attachments
view.phtml  (File Size: 2KB - Downloads: 129)
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 3 of 3