Magento – Load Simple Products Collection (both, in stock and out of stock)

associate-productscollection;configurable-productproductproduct-collection

I got a problem loading ALL "child" products of a Configurable Product into a collection, even those that are out of stock.

Like loading the products like this:

$simpleCollection = $configurable->getUsedProductCollection()
    ->addAttributeToSelect('*')
    ->addFilterByRequiredOptions();

foreach ($simpleCollection as $simple) {
   //$simple->getName();
}

will ignore child products that are out of stock, probably because they're not listed in the price tables, that are joined.

Is there another option without loading all child id's by getChildrenIds and then load each simple product with load?

Best Answer

The problem lies in the call to addStoreFilter() in getUsedProductCollection():

public function getUsedProductCollection($product = null)
{
    $collection = Mage::getResourceModel('catalog/product_type_configurable_product_collection')
        ->setFlag('require_stock_items', true)
        ->setFlag('product_children', true)
        ->setProductFilter($this->getProduct($product));
    if (!is_null($this->getStoreFilter($product))) {
        $collection->addStoreFilter($this->getStoreFilter($product));
    }

    return $collection;
}

This adds filters to only show products that are salable in the current store.

If $configurable is the type instance of your configurable product, you can unset the store filter like this before calling getUsedProductCollection():

$configurable->setStoreFilter(null);

Complete solution:

$configurable = $product->getTypeInstance();

$configurable->setStoreFilter(null);
$simpleCollection = $configurable->getUsedProductCollection()
    ->addAttributeToSelect('*')
    ->addFilterByRequiredOptions();

foreach ($simpleCollection as $simple) {
   //$simple->getName();
}
Related Topic