odd_duck
odd_duck

Reputation: 4101

Magento - Select stock status in category->getProductCollection

I am trying to get the stock status of all products within a particular category. My current code is below but getIsInStock is not part of the default collection. How can i get this value in the same way as ->addAttributeToSelect('product_type')

$collection = Mage::getModel('catalog/category')->load($_cat_id)->getProductCollection()
->addAttributeToSelect('product_type');

foreach ($collection as $product) {
    if( $product->getIsInStock() ) { 
        print $product->getProductType().' is in stock';
    }
}

Upvotes: 0

Views: 226

Answers (1)

You can join your collection by :

$collection->joinField(
                        'is_in_stock',
                        'cataloginventory/stock_item',
                        'is_in_stock',
                        'product_id=entity_id',
                        '{{table}}.stock_id=1',
                        'left'
                        )

let me know if it helpes

Upvotes: 1

Related Questions