This should work to quickly grab everything you want. There may be a better way to do it.
<?php
// Load Magento
require_once '../app/Mage.php';
umask(0);
Mage::app('default');
//get the collection filter the simple products & join the cataloginventory/stock_item table
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('sku')->addAttributeToFilter('type_id', 'simple')->joinField(
'qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left'
) ;
?>
<html>
<head></head>
<body>
<table>
<?php
foreach ($collection as $product) {
echo "<tr><td>".$product->getSku()."</td><td> ".(int)$product->getQty()."</td></tr>";
};
?>
</table>
</body></html>
Here is an example how to access the is salable state or the stock quantity of all associated child products.
this first method is simply to fetch the child product ids from the bundled product.
function getBundledProductChildIds(Mage_Catalog_Model_Product $product)
{
$childrenIds = [];
if ($product->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) {
$childrenIds = array_reduce(
$product->getTypeInstance(true)->getChildrenIds($product->getId()),
function (array $reduce, $value) {
return array_merge($reduce, $value);
}, []);
}
return $childrenIds;
}
The next example gives you the in stock or out of stock for each child product.
$bundledProduct = Mage::getModel('catalog/product')->load(BUNDLED_PRODUCT_ID);
// Just get the is salable state of the child products
$childProducts = Mage::getResourceModel('catalog/product_collection')
->addIdFilter(getBundledProductChildIds($bundledProduct));
foreach ($childProducts as $child) {
printf("%s -> %s stock\n", $child->getId(), ($child->getIsSalable() ? 'in' : 'out of'));
}
The final example gives you the stock qty of each child product.
Note that the key difference is that the require_stock_items
flag is set on the collection before it is loaded.
$bundledProduct = Mage::getModel('catalog/product')->load(BUNDLED_PRODUCT_ID);
// Get the inventory qty of each child item
$childProducts = Mage::getResourceModel('catalog/product_collection')
->addIdFilter(getBundledProductChildIds($bundledProduct))
->setFlag('require_stock_items', true);
foreach ($childProducts as $child) {
printf("%s -> stock qty: %d\n", $child->getId(), $child->getStockItem()->getQty());
}
printf("Total stock item qty sum: %d\n", array_reduce(
$childProducts->getItems(),
function ($sum, $child) {
return $sum + $child->getStockItem()->getQty();
}, 0));
printf("Highest stock qty: %d\n", array_reduce(
$childProducts->getItems(),
function ($max, $child) {
return max($max, $child->getStockItem()->getQty();
}, 0));
That should hopefully enable you to get what you need.
Best Answer
Use the following for all types of products in order to check the stock:
Haven't checked but you can try this as well: