第二级别类别中的getProductCount()回显计数打印0,我尝试不同的方式,但我没想到像法师得到收集等,我没有在这个问题找到任何解决方案
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<?php foreach ($collection as $cat):?>
<?php
$cur_category = Mage::getModel('catalog/category')->load($cat->getId());
$_img = $cur_category->getThumbnailUrl();
?>
<div class="grid_4">
<div class="mineContent_grid_4">
<dl>
<dt>
<a href="<?php echo $helper->getCategoryUrl($cat);?>">
<?php echo $cat->getName();?>
<img src="<?php echo $_img?>" title="<?php echo $cat->getName();?>" width="173" height="208"/>
</a>
</dt>
<?php $childLevel2Category = Mage::getModel('catalog/category')->getCategories($cat->entity_id);
?>
<dd>
<ol>
<?php foreach ($childLevel2Category as $catLevel2) { ?>
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
$count = $cur_category2->getProductCount();
?>
<li> <a href="<?php echo $helper->getCategoryUrl($catLevel2);?>"><?php echo $catLevel2->getName();?> <span>(<?php echo $count ?>)</span></a></li>
<?php } ?>
</ol>
</dd>
</dl>
</div>
</div>
<?php endforeach;?>
答案 0 :(得分:5)
您有以下代码:
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
$count = $cur_category2->getProductCount();
?>
这会加载$cur_category2
$cat->getId()
,这是您的父类,而不是当前类别。我想你想要这个:
<?php
$cur_category2 = Mage::getModel('catalog/category')->load($catLevel2->getId());
$count = $cur_category2->getProductCount();
?>
答案 1 :(得分:0)
这是一个可以帮助你的片段。我的所有代码片段都是获取该类别的产品数量。我将类别ID硬编码为4,但您的代码应该有效,获取当前类别。您可能希望将此代码隔离到一个函数中以使其更简单,然后只需从现有页面引用它。它基本上按类别加载一系列产品,过滤掉不可见的产品。
$_category = Mage::getModel('catalog/category')->load(4);
$collection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($_category);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
echo $collection->count();
答案 2 :(得分:-1)
试试这个例子
$categories = Mage::getModel('catalog/category')->load(2)->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addCountToCategories($categories);
var_dump($categories);
其中2 - 类别ID 还要检查Magento中的php class Mage_Catalog_Block_Navigation