Magento getProductCount() 子类别
第二级类别回显计数中的 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;?>
the getProductCount() in the second level of category echo count print out 0, i try different way but i diddn't figure out like mage get collection etc, i didn'd find any solution at this question
<?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;?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有以下代码:
这会使用
$cat->getId()
加载$cur_category2
,这是您的父类别,而不是当前类别。我想你想要这个:You have this code:
This loads
$cur_category2
with$cat->getId()
, which is your parent category and not the current category. I think you want this:这是一个应该对您有帮助的片段。我的代码片段所做的只是获取该类别的产品数量。我硬编码了类别 ID 4,但您的代码应该可以工作,获取当前类别。您可能希望将此代码隔离到一个函数中以使其更简单,然后只需从现有页面引用它即可。它本质上是按类别加载产品集合,过滤掉不可见的产品。
Here's a snippet that should help you. All my snippet does is get the product count for the category. I hard coded a category ID of 4, but your code should work, getting the current category. You may want to isolate this code into a function to keep it simpler, then just reference it from your existing page. It essentially loads a collection of products, by category, filtering out products that aren't visible.
试试这个例子
,其中 2 - 类别 id
还要检查 Magento 中的 php 类 Mage_Catalog_Block_Navigation
try this example
where 2 - category id
also check php class Mage_Catalog_Block_Navigation in Magento