I'm trying to rewrite some poor performing code in a third party extension that loads all simple products from a configurable product and displays them for purchase at once in a grid. They are calling Mage::getModel('catalog/product')->load() in a foreach loop after getting all the simple product IDs, which is pretty bad practice so I've read. I've tried to replace this by loading the entire collection like so:

$_subproducts = Mage::getModel('catalog/product')->getCollection()->addIdFilter($_allChilds)->addAttributeToSelect('*')

The problem is this ends up giving me a bunch of NULL attributes, no stock data, and image data that is formed differently, for instance there is no "image" key which they use to display the simple product image. I've managed to get the image data loaded by doing the following inside the foreach loop:

$confSimpleProduct = $_subproducts->getItemById($spd);
$attributes = $confSimpleProduct->getTypeInstance(true)->getSetAttributes($confSimpleProduct);
$media_gallery = $attributes['media_gallery'];
$backend = $media_gallery->getBackend();

I still have not figured out how to get stock information loaded and because of all the other helper methods being called it would be great to just have a drop in way to replace this so that I don't have to rewrite/debug lots of the code.


It's working for me.

foreach($products as $_product){

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();


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.