根据分类属性来获取目录
2010-09-03 15:06:06.0
magento中获取分类还是比较简单的
1.根据分类的ID来获取
Mage::getModel('catalog/category')->load('分类ID')
2.根据分类的属性来获取分类
Mage::getModel('catalog/category')->loadByAttribute('name','栏目名');
我们看下方法名就知道了可以用任何属性来查找当前分类..
通过栏目的url来来获取分类
Mage::getModel('catalog/category')->loadByAttribute('url_key', 'the-key-goes-here');
获取子分类中的方法,看我以前的文章:magento获取子分类
为了实现magento根据分类或者栏目的属性来获取分类or栏目.在获取该分类或者目录下的所有子分类
我们可以根据上面的loadByAttribute来获取父分类,再根据前面我的文章来获取所有的子分类
最终代码
<?php
$c=Mage::getModel('catalog/category');
$pc=$c->loadByAttribute('name','选购礼盒');
if($pc->hasChildren()) {
$ids = $pc->getChildren();
$subCategories = Mage::getModel('catalog/category')->getCollection();
$subCategories->getSelect()->where("e.entity_id in ($ids)");
$subCategories->addAttributeToSelect('*');
$subCategories->load();
}
?>
再循环打印出分类的别名和url,这里我是利用了分类的描述这个属性
<?php foreach($subCategories as $item): ?> <li><a href="<?php echo $item->getUrl();?>"><?=$item['description']?></a></li> <?php endforeach; ?>
文章作者:POPO4J
本文地址:http://www.popo4j.com/magento/property_to_obtain_category.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!