4

我正在尝试显示每个类别中的产品数量,使用此代码来显示 category 的子类别id:3。它正在显示,但它包括禁用和隐形产品。

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
?>
<ul>
<?php foreach($cats as $category): ?>
<li>
<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?>(<?php echo $category->getProductCount(); ?>)</a>
</li>
<?php endforeach; ?>
</ul>

是否有任何好的解决方案,以便我可以获得已启用和活动的类别的确切数量。

4

3 回答 3

3
you need check active filter acondition and for this you can use below code 


    $products = Mage::getModel('catalog/category')->load($category->getId())
        ->getProductCollection()
        ->addAttributeToSelect('entity_id')
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('visibility', 4);

    echo $products->count();
于 2013-05-31T13:53:04.127 回答
1

在不考虑 Magento 编程约定的情况下,这段代码应该如下所示:

<?php
$cats = Mage::getModel('catalog/category')->load(3)->getChildrenCategories();
$collection = Mage::getModel('catalog/product')->getCollection();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
?>
<ul>
<?php foreach($cats as $category): ?>
<?php $count = $collection->addCategoryFilter($category)->getSize(); ?>
<li>
<a href="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?>(<?php echo $count ?>)</a>
</li>
<?php endforeach; ?>
</ul>

但是,帮世界一个忙,正确地组织代码。

于 2013-05-31T13:49:26.003 回答
0

加载集合和使用多属性过滤器不是正确的方法,这不会过滤关联的产品及其库存水平。

这段代码一起做,

$categoryId = 32; // Replace with your category

$category = Mage::getModel('catalog/category')
                 ->setStoreId(Mage::app()->getStore()->getId())
                 ->load($categoryId);
Mage::register('current_category', $category);

$products = Mage::getSingleton('catalog/layer')->getProductCollection();

echo $products->getSize();
于 2016-02-29T12:10:14.043 回答