Question

I'm trying to get the first 4 products from a category that are in stock to show on the home page. I've got everything working the way I would like except it's pulling products that are out of stock.

I've tried addAttributeToFilter('is_in_stock', 1) and isSaleable() but those didn't work.

$collection = Mage::getModel('catalog/category')->
                    load('33')->
                    getProductCollection()->
                    addAttributeToFilter('status', 1)->
                    addAttributeToSelect('*')->
                    addAttributeToSort('position')->
                    setPageSize(4);
Was it helpful?

Solution

You have to pass the collection through a seperate method for that

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

But maybe you would be better of using the regular list.phtml including it by using the XML layout in the page CMS. that way you don't need to write any code. The snippet below should work

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
   <action method="setCategoryId"><category_id>30</category_id></action>
   <action method="setLimit"><value>4</value></action>
</block>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top