我想在Magento 1.8中显示单个类别中有多少活跃产品。
我需要在.phtml文件中显示一些选择的类别,并希望显示这些类别中有多少产品。
答案 0 :(得分:0)
通过可见性计算所有产品数量=目录搜索,目录和状态=已启用
<?php
$_category = Mage::getModel('catalog/category')->load(3); // 3 is the category id here
$_product = $_category->getProductCollection();
$_product->setVisibility(array(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG));
$_product->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
echo $_product->count();
&GT;
答案 1 :(得分:0)
对于 Magento 2,我们应该看看这个:
vendor/magento/module-catalog-graph-ql/Model/Resolver/Category/ProductsCount.php
$productsCollection = $category->getProductCollection();
$productsCollection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
$productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []);
return $productsCollection->getSize();