2

Lets say I do the following:

$prod = Mage::getModel('catalog/product');
$prod->load(243);
Mage::log($prod->getStockItem()->getIsInStock());

I get 1 (eg function returns true)

However, if I do this:

$productCollection = Mage::getModel('catalog/product')->getCollection()
                            ->setPageSize($pageSize)->setCurPage($pageIndex)
                            ->addAttributeToSelect('*')->load();

then I iterate over the products and do

Mage::log($prod->->getStockItem()->getIsInStock());

I always get false, even for item 243 which should return true.

Why is that? and how do I load the stock item data?

(what I care about is if item is really in stock)

Thanks!

4

If you want your collection to include the inventory information, there's a flag you need to set before you load the collection.

$productCollection->setFlag('require_stock_items', true)

| improve this answer | |
2

Product stock is a different table(cataloginventory_stock_item). Try this:

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left')
    ->addAttributeToFilter(
        'qty', 
        array("gt" => 0)
    );
| improve this answer | |
1

Notice the double arrows ->-> after $prod

Mage::log($prod->->getStockItem()->getIsInStock());

You are probably thinking along the lines that a product should be loaded completely when it is part of the collection. It seems that it isn't. I think it's the whole EAV and storing data in different tables.

So you can use Richard's suggestion also loads the full stock_items

$productCollection->setFlag('require_stock_items', true)

Or you can join the is_in_stock column to your product in the collection

$productCollection = Mage::getModel('catalog/product')->getCollection()
                        ->setPageSize($pageSize)->setCurPage($pageIndex)
                        ->addAttributeToSelect('*')
                        ->joinField(
                            'is_in_stock',
                            'cataloginventory/stock_item',
                            'is_in_stock',
                            'product_id=entity_id',
                            '{{table}}.stock_id=1',
                            'left')
                        ->load();

Or join a table with the columns from stock data that you need.

 $productCollection = Mage::getModel('catalog/product')->getCollection()
                            ->setPageSize($pageSize)->setCurPage($pageIndex)
                            ->addAttributeToSelect('*')
                            ->joinTable(
                                    'cataloginventory/stock_item',
                                    'product_id=entity_id',

                                    //columns needed
                                    array('stock_id','qty', 'min_qty', 'backorders', 'is_in_stock')
                            )
                            ->load();

If you need to see the columns of stock_item table, execute this query in your database

use database_name;
show columns from cataloginventory_stock_item;

A couple of debugging tips. If you use ->load(true) at the end of the collection, you will get the SQL query that was executed.

You can also print all the data of a product by using getData()

// gets the first item of the collection and print its data
var_dump($productCollection->getFirstItem()->getData());

In your example, within the iteration:

Mage::log($prod->getData());

A bit off topic, if you are adding items to a Quote/Cart and you get the error below you can use Richard's suggestion when you load the collection.

The stock item for Product is not valid.

| improve this answer | |
0

Try this:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*');

if($collection->getSize()) {
    foreach($collection as $product) {
        echo 'Product Name : '. $product->getName().'<br>';
        echo 'Product Stock : '. $product->getStockItem()->getIsInStock().'<br><br>';
    }           
}
| improve this answer | |

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.