0

I'm fairly new to Magento and came across a situation where I need to get data (specifically available quantity) of all simple products connected to its configurable product.

I've managed to get the id's of the simple products like this:

$simpleProductIds = Mage::getModel('catalog/product_type_configurable')->getUsedProductIds($product);

This gives me an array with all the ids of the simple products linked to the configurable product. But now I'd like to get the quantities of all these simple products (to put in array and later check their availability, as requirement due to custom add to cart action). I'd like to do this in a single request so as to minimize the load.

What I've got so far (and doesn't work), is the following:

$stockItemCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()
            ->addAttributeToFilter('product_id', array('in' => $simpleProductIds));

I've also tried it with ->addAttributeToSelect and both versions with the following after it:

->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
 );

(as found in this entry)

So far none of these things have yielded any results. What am I missing/doing wrong?

Please note that once I have the simple product ids (which works as desired), I want to get all the simple products with only one database call, which I know is possible, somehow (if all of this can be done with just one database call, instead of first getting all the id's, that would be even better).

Any wizard know this magic spell? Many thanks.

2
  • Deleted my answer. Did not read. Sorry. On mobile. So skimmed it.
    – ProxiBlue
    Commented Nov 21, 2016 at 7:33
  • you can get more help from this link1 ,link2 Thanks Commented Nov 21, 2016 at 10:45

1 Answer 1

0

Okay, after some more digging and fiddling, I've found a solution. Maybe not the best, but this works for me:

$simpleProductIds = Mage::getModel('catalog/product_type_configurable')->getUsedProductIds($product);

$simpleProductColl = Mage::getModel('catalog/product')->getCollection()
    ->addIdFilter($simpleProductIds)
    ->addAttributeToSelect('*')
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );

Thought it seems preferable to replace the '*' from ->addAttrubuteToSelect('*') with an array of only the attributes you want.

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.