Magento getProductCount() 子类别

发布于 2025-01-05 07:48:33 字数 1645 浏览 1 评论 0原文

第二级类别回显计数中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌入人心 2025-01-12 07:48:33

您有以下代码:

<?php
   $cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
   $count = $cur_category2->getProductCount();
?>

这会使用 $cat->getId() 加载 $cur_category2,这是您的父类别,而不是当前类别。我想你想要这个:

<?php
   $cur_category2 = Mage::getModel('catalog/category')->load($catLevel2->getId());
   $count = $cur_category2->getProductCount();
?>

You have this code:

<?php
   $cur_category2 = Mage::getModel('catalog/category')->load($cat->getId());
   $count = $cur_category2->getProductCount();
?>

This loads $cur_category2 with $cat->getId(), which is your parent category and not the current category. I think you want this:

<?php
   $cur_category2 = Mage::getModel('catalog/category')->load($catLevel2->getId());
   $count = $cur_category2->getProductCount();
?>
戈亓 2025-01-12 07:48:33

这是一个应该对您有帮助的片段。我的代码片段所做的只是获取该类别的产品数量。我硬编码了类别 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();

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.

$_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();
-小熊_ 2025-01-12 07:48:33

试试这个例子

$categories = Mage::getModel('catalog/category')->load(2)->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addCountToCategories($categories);

var_dump($categories);

,其中 2 - 类别 id
还要检查 Magento 中的 php 类 Mage_Catalog_Block_Navigation

try this example

$categories = Mage::getModel('catalog/category')->load(2)->getChildrenCategories();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$productCollection->addCountToCategories($categories);

var_dump($categories);

where 2 - category id
also check php class Mage_Catalog_Block_Navigation in Magento

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文