前台显示Category定制属性两例

本文介绍如何在Magento中为Category模型增加promo_name属性,并在Category列表页及树状结构中显示此属性。通过修改加载Category节点的方法,在获取Category树时加入自定义属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因业务需要,Category增加了一个属性叫promo_name,进入Category列表页时希望将其显示出来,这里的情况具体分两种:

I. load方法访问自定义属性
$c = Mage::getModel('catalog/category');
echo $c->getPromoName();

这个是最自然的用法,麻烦的是下面的情况:

II. 首先得到了树状的Category(不是平面的), 看下面代码示例
   public function getCategoryNodes($parentId = null,  $sorted=false, $asCollection=false, $toLoad=true) {
$config = Mage::getModel('catalogue/joyconfig');
if(empty($parentId)) {
$parentId = $config->getRootBrandId();
}
$category = Mage::getModel('catalog/category');
/* @var $category Mage_Catalog_Model_Category */
if (!$category->checkId($parentId)) {
if ($asCollection) {
return new Varien_Data_Collection();
}
return array();
}

$recursionLevel = max(0, 0);

$tree = $category->getTreeModel();
$nodes = $tree->loadNode($parentId)
->loadChildren($recursionLevel)
->getChildren();

$tree->addCollectionData(null, $sorted, $parentId, $toLoad, true);

if ($asCollection) {
return $tree->getCollection();
} else {
return $nodes;
}
}

其功能是:给定一个category_id,返回该id下所有子分类以树状结构返回,为了让返回的每个节点(Category Node)包含该定制属性, 在代码
$tree->addCollectionData(null, $sorted, $parentId, $toLoad, true);

前增加如下的行:
$tree->getCollection($sorted)->addAttributeToSelect('promot_name');

就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值