我正在尝试使用子类别过滤产品列表,让我说我有一个名为perfume
的顶级类别和名为for him
的子类别& for her
。在顶级类别列表页面中,我正在尝试包含名为filter results
的选项,它应显示下拉列表for men and for women
如果用户点击for men
,则应仅显示for men category
要做到这一点,我尝试了,
<?php $categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('my_attribute')
->addAttributeToSelect('position')
->addAttributeToSort('position', 'ASC')
->setLoadProductCount(true)
->addAttributeToFilter('is_active',array('eq'=>true))
->load(); ?><?php foreach($categories as $key=>$category): ?><?php if($category->getName() != ''):?> <?php $prodCollection = age::getResourceModel('catalog/product_collection')->addCategoryFilter($category); // Magento product collection ?>
<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a> (<?php echo $prodCollection->count() ?>)<br/>
但它显示了所有类别的产品数量。 有没有人知道如何过滤这个只显示当前主要类别的子类别?
谢谢,
答案 0 :(得分:0)
<?php
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootCategoryId);
?>
<?php foreach($categories as $category): ?>
<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a><br/>
<?php endforeach ?>