9

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?

2

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();
}
1

What happens if you try this way:

$simpleCollection = $configurable->getUsedProductCollection()
                     ->addAttributeToSelect('*')
                     //->addFilterByRequiredOptions() //don't use any filter, get all itmes
                     ;


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

Give it a try.

1

You can use below code as alternative solution for this question:

$simpleCollection=$configurable->getTypeInstance(true)
                ->getUsedProducts(null,$configurable);

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

If you need the collection of products associated to a configurable, the following should work:

$configurableProduct = Mage::getModel('catalog/product')->load(<your_product_id>);
$associatedProducts = $configurableProduct->getTypeInstance()->getUsedProductCollection($configurableProduct);

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.