2
votes

Hi I'm new to magento and have been trying to set up a static block that displays a list of sub categories within a category. I've been succesfull a grabbing the sub-category images and names, but for some reason I can't seem to get the descriptions to show.

Here's the code can't anyone explain why it won't work and how I can fix it?

I've commented out a few lines because I was trying different things to get it to work.

helper('catalog/output'); $category = $this->getCurrentCategory(); getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?> <?php  echo 

$this->htmlEscape($_category->getCategoryDescription());?>

        <?php if($_category->getIsActive()): ?>

            <div class="subcategory-image">

                        <a href="<?php echo $_category->getURL()
?>" title="htmlEscape($_category->getName()) ?>">
                        </a>
                            <?php /* echo "Find this item->" */ ?>

                    </div> <div class="sub-category-container">
                    <h2><a href="<?php echo $_category->getURL()
?>" title="htmlEscape($_category->getName()) ?>">htmlEscape($_category->getName()) ?> getURL() ?>" class="moreLink">[MORE...] getDescription() ?>--> getDescription()): ?> categoryAttribute($_category, $_description, 'description'); ?> -->

2
You'll have a lot more luck getting a response if you format your code in a reasonable way.Alan Storm

2 Answers

10
votes

This is one of those cases where Varien decided that they should call "load" on the data collection before returning it when that really isn't necessary and makes the utility function utterly useless.. If you trace the code down for Mage_Catalog_Block_Navigation->getChildrenCategories() you will eventually find this in Mage_Catalog_Model_Resource_Eav_Mysql4_Category:

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

The next to last line ->load(); means that the query is executed and the collection is loaded so it is too late to modify the query. So what you will want to do is copy and paste that in place of calling getChildrenCategories and then add the additional attributes you want to use like so:

$_categories = $category->getCollection()
    ->addAttributeToSelect(
        array('url_key','name','all_children','is_anchor','description')
    )
    ->addAttributeToFilter('is_active', 1)
    ->addIdFilter($category->getChildren())
    ->setOrder('position', 'ASC')
    ->joinUrlRewrite()
;

Now the collection will include the description attribute so that getDescription() will work. Notice that you do not need to call load(), it happens automatically when you start using the iterator (the foreach loop triggers this). This is why it is pointless for the load() call to be included in that function because otherwise you could have just added one line below the function call:

$categories->addAttributeToSelect('description');

But instead you have to copy the contents of the function to tweak the query.

-1
votes

Change:

$_category->getCategoryDescription()

To this:

$_category->getDescription()