MAGENTO从子类别中获取描述
嗨,我是magento的新手,一直在尝试设置一个静态块,显示类别中的子类别列表。我已经成功地抓取了子类别的图片和名称,但由于某种原因,我似乎无法得到描述显示 这是代码,没人能解释为什么它不起作用,以及我如何修复它吗 我已经注释掉了几行,因为我尝试了不同的方法来让它工作 助手(“目录/输出”); $category=$this->getCurrentCategory(); getCurrentChildCategories(); ?>MAGENTO从子类别中获取描述,magento,Magento,嗨,我是magento的新手,一直在尝试设置一个静态块,显示类别中的子类别列表。我已经成功地抓取了子类别的图片和名称,但由于某种原因,我似乎无法得到描述显示 这是代码,没人能解释为什么它不起作用,以及我如何修复它吗 我已经注释掉了几行,因为我尝试了不同的方法来让它工作 助手(“目录/输出”); $category=$this->getCurrentCategory(); getCurrentChildCategories(); ?> 这是其中一种情况,Varien决定在返回数据收集之前调用“
这是其中一种情况,Varien决定在返回数据收集之前调用“load”,而这实际上是不必要的,并且使实用程序功能完全无用。。如果您跟踪Mage\u Catalog\u Block\u Navigation->getChildrenCategories()
的代码,您最终会在Mage\u Catalog\u Model\u Resource\u Eav\u Mysql4\u 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
,然后添加您想要使用的其他属性,如:
$_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()
调用是毫无意义的,因为否则您可能只会在函数调用下面添加一行:
$categories->addAttributeToSelect('description');
但是,您必须复制函数的内容来调整查询。更改:
$\u类别->getCategoryDescription()
为此:
$\u category->getDescription()如果您以合理的方式格式化代码,您将有更多的机会得到响应。
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;
}
$_categories = $category->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite()
;
$categories->addAttributeToSelect('description');