Question

I use bellow code to show number of articles from the category number 4 (in my case)! Magento is showing 65 articles from that category, but in the stock there are only 36 articles. What you would like to achieve is to show number of articles which are in stock. I don't want to include articles from out of stock.

<?php 
$products_count = Mage::getModel('catalog/category')->load(4)
->getProductCount();
echo($products_count);
?> 
Was it helpful?

Solution

Try this way

<?php

$category = Mage::getModel('catalog/category')->load(4);
$productCollection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('name', 'image', 'price'))
                        ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

$productCollection->addCategoryFilter($category);

Mage::getSingleton('cataloginventory/stock')
    ->addInStockFilterToCollection($productCollection);

echo $productCollection->getSize();

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top