2

In some cases you need the simples belonging to a configurable, or specific attributes of them.

Example1: the product types

Example2: the prices

Example3: 3 attributes at once (I assume this is harder to get performantly done)

1

1 Answer 1

5

I've recently been struggling to find a performant way to achieve Example3's scenario while working on a script to validate/check some data.

The script inits mage and among other things grabs a collection of product with a matching attribute set - the following shows how $_products is instantiated in my example:

$_products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldToFilter( 'attribute_set_id', '9' )
    ->addFieldToFilter( 'type_id', 'configurable' );

Initially I tried something along these lines:

foreach ( $_products as $product ) {
    $child_ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
    foreach ( $child_ids[0] as $id ) {
        $child = Mage::getModel('catalog/product')->load($id);

Which is most certainly not performant as it loads additional models within yet another loop. Then I stumbled upon the following that seems to be a bit more performant:

foreach ( $_products as $product ) {
    $children = $product->getTypeInstance()->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    foreach ( $children as $child ) {

I haven't yet had the chance to benchmark the differences but it seems noticeably faster, will log some stats and expand this answer when I get a chance!

2
  • Trade that * for array('attr_code1','attr_code2',[...]) and you've got it.
    – benmarks
    Feb 19, 2015 at 4:29
  • Swapping * for an array of attributes requires you to run a query first to get the attributes (of the given attribute set) you need to return, right, assuming you don't know what attributes exist/need/want? So is that quicker than getting ALL attributes? Feb 19, 2015 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.