Question

I have changed option in Magento to show only products which are in stock. Configuration -> Catalog -> Inventury. DISPLAY OUT OF STOCK PRODUCTS is set to "NO"

On my front/index page I use bellow code to display randomly 5 products from same category.

<?php 
$products = Mage::getModel('catalog/product')->getCollection();
$products->joinField(
'qty', 
'cataloginventory/stock_item', 
'qty', 
'product_id=entity_id', 
'{{table}}.stock_id=1', 
'left' 
);
$products->addAttributeToSelect(array('name', 'thumbnail', 'price', 'special_price' , 'RegularPrice' )); 
$products->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id=entity_id', null, 'left' );
$products->getSelect()->limit(5);
$products->getSelect()->order('RAND()'); 
$products->addAttributeToFilter('category_id', array('in' => array(5))); 
$this->_reviewsHelperBlock = $this->getLayout()->createBlock('review/helper'); 
foreach ($products as $product)  : 
?>

Above code displays products which are in stock and products which are out of stock.

What I would like to accomplish is to display products which are only in stock. I have tried to do this in so many different ways but have not succeeded.

Was it helpful?

Solution

Hy the stock information is managed by the cataloginventory module, try to add the following code on your products collection

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

regards

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