0

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.

1 Answer 1

2

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

2
  • I try adding code you provided. I received the following error: Fatal error: Call to a member function joinField() on a non-object in httpdocs/app/code/core/Mage/CatalogInventory/Model/Resource/Stock.php on line 197
    – user15151
    Commented Oct 28, 2015 at 15:22
  • 1
    actually this works. My bad.
    – user15151
    Commented Oct 28, 2015 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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