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.

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

| improve this answer | |
  • 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 Oct 28 '15 at 15:22
  • 1
    actually this works. My bad. – user15151 Oct 28 '15 at 16:17

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.