MAGENTO从子类别中获取描述 [英] MAGENTO getting the descriptions from a sub category

查看:89
本文介绍了MAGENTO从子类别中获取描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是magento的新手,并一直试图设置一个静态块来显示类别中子类别的列表.我已经成功地获取了子类别的图像和名称,但是由于某种原因,我似乎无法显示其描述.

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('目录/输出'); $ category = $ this-> getCurrentCategory(); getCurrentChildCategories(); ?>
helper('catalog/output'); $category = $this->getCurrentCategory(); getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?> <?php  echo 

$ this-> htmlEscape($ _ category-> getCategoryDe​​scription());?>

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

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

            <div class="subcategory-image">

                        <a href="<?php echo $_category->getURL()

?>"title =" htmlEscape($ _ category-> getName()) ?>>

?>" 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"> [更多...] getDescription()?>-> getDescription()): ?> categoryAttribute($ _ category,$ _description,'description'); ?>

?>" title="htmlEscape($_category->getName()) ?>">htmlEscape($_category->getName()) ?> getURL() ?>" class="moreLink">[MORE...] getDescription() ?>--> getDescription()): ?> categoryAttribute($_category, $_description, 'description'); ?>

推荐答案

在这种情况下,Varien决定在真正不需要的时候返回数据集之前先对数据集调用加载",然后实用程序功能完全没有用.如果您跟踪Mage_Catalog_Block_Navigation->getChildrenCategories()的代码,最终将在Mage_Catalog_Model_Resource_Eav_Mysql4_Category中找到它:

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;
}

最后一行->load();的下一行表示已执行查询并加载了集合,因此修改查询为时已晚.因此,您要做的是复制并粘贴该内容以代替调用getChildrenCategories,然后添加要使用的其他属性,如下所示:

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()
;

现在,该集合将包含description属性,以便getDescription()可以工作.请注意,您不需要调用load(),当您开始使用迭代器时,它会自动发生(foreach循环会触发此事件).这就是为什么load()调用包含在该函数中毫无意义的原因,否则您可能只是在该函数调用下方添加了一行:

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.

这篇关于MAGENTO从子类别中获取描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆