Get random products that are in stock
-
01-01-2021 - |
Question
This is my code, i get random products here ...
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect(array('name', 'thumbnail', 'price')); //feel free to add any other attribues you need.
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getModel('cataloginventory/stock_item')->loadByProduct($products);
$products->getSelect()->order('RAND()');
$products->getSelect()->limit(6);
... but this code show out of stock products too. I need just show in stock (available) products here. How can i do that?
La solution
You can try
Mage::getSingleton('cataloginventory/stock')
->addInStockFilterToCollection($products);
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange