0

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);
?> 
1

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();

?>
  • Working perfect :) – user15151 Oct 27 '15 at 17:11
  • great, you can up-vote answer as well :) – Akhilesh Patel Oct 27 '15 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.