开发者

Magento displays subcategories description on category list.phtml

I got a category page with list of subcategories in Magento. The list has an image and name, but I also want to display those subcategories' description. I tried simply to add

<strong><?php echo $this->htmlEscape($_category->getDescription()) ?></strong>

but it's not working.

EDIT: I get subcategories in a traditional way:

<?php if (!$_categoryCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no subcategories matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php $_collectionSize = $_categoryCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_categoryCollection as $_category): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $_category->getUrl() ?>" class="product-image"><img class="photo" src="<?php echo $this->getCategoryImage($_category, 214, 184); ?>" width="214" height="184" alt="<?php echo $_category->getName() ?>" />
                <strong><?php echo $this->htmlEscape($_category->getName()) ?></strong>
                <strong><?php echo $_category->getDescription() ?></strong>
                </a>
            </li>
        <?php if ($i%$_columnCou开发者_Python百科nt==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
    <?php endforeach ?>
</div>

I've tried to update the public function getChildrenCategories($category) in the category.php file by adding ->addAttributeToSelect(’description’), but it's not working.


I can't see what exactly it is that you're doing wrong, but perhaps I can still help. I have successfully displayed the child category descriptions in list.phtml and you may be able to adapt what's working for me to your own purposes. The following is a stripped-down version of the code which works for me:

<?php $children = explode( ",", $this->getCurrentCategory()->getChildren() ); ?>
<div class="category-products">
    <ul class="products-grid">
        <?php foreach( $children as $child ): ?>
            <?php $_child = Mage::getModel( 'catalog/category' )->load( $child ); ?>
            <li class="item"><?php echo $_child->getDescription(); ?></li>
        <?php endforeach; ?>
    </ul>
</div>

The big difference between what you're doing and my sample above is that the getChildren() method on the catalog model object returns an array of category Ids and I then use the category Ids to load the corresponding child category model instances. My memory may be wrong here, but I seem to remember that the items returned from a Magento collection don't contain the full data that you get when you load by id.

I'm not sure if this will affect performance significantly or not (I would assume that loading a collection is faster than loading individual models) but it works so I'm not complaining...

Hope this helps.

Cheers, Zac


I have relatively the same idea on the website I'm working on, where I display sub-categories in a grid view. While I used to use the method of individually loading category/product information by id's, I sort of fell in love with using the "Mage::getModel('')->getCollection()" method.

This is what I've used on my sub categories, and I've been quite happy with it, as it grabs all the information at once:

<?php
    if(Mage::registry( 'current_category' )->getId()) {

        $_currentCategoryId = Mage::registry( 'current_category' )->getId();

    } else {    //Get Root Category Id if not in a category

        $_currentCategoryId = Mage::app()->getStore()->getRootCategoryId();
    }

    $_subCategories = Mage::getModel( 'catalog/category' )->getCollection()
                        ->addAttributeToSelect('*')
                        ->addFieldToFilter('parent_id',array('eq' => $_currentCategoryId))
                        ->addFieldToFilter('include_in_menu',array('eq' => '1'))
                        ->addFieldToFilter('is_active', array('eq' => '1'))
                        ->addAttributeToSort('position', 'asc');
?>
<?php if(count($_subCategories) > 0): ?>

<!--This is where the sub-category layout would go.-->

<?php else: ?>

<!--Do something else if there are no sub-categories.-->

<?php endif; ?>

This will grab all visible sub-categories of the current category, OR grab the base categories (from Root Category Id) of the store, if you choose to have the template show on any other page. You could go further and define specific attributes with addAttributeToSelect as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜