Magento – How to get configurable child product by attribute

magento-1.9

I have a configurable product "T-Shirt" with id = 1.

I want to get the object for the simple product with size of "medium" so I can then query the stock value.

What is the syntax for:

"Get the medium value of attribute size from configurable parent
T-shirt"

Best Answer

Get configurable child product by attribute:-


$product= Mage::getModel('catalog/product')->load($configId);
    #Check if the product has children
    if ($product->type_id == 'configurable') {
        $options = array();
        // Get any super_attribute settings we need
        $productAttributesOptions = $product->getTypeInstance(true)->getConfigurableOptions($product);
        foreach ($productAttributesOptions as $productAttributeOption) {
                    $options[$product->getId()] = array();
                foreach ($productAttributeOption as $optionValues) {
                    $val = $this->trimValue($optionValues['option_title']);
                    $options[$product->getId()][] = array (
                        $optionValues['sku'] =>  $optionValues['attribute_code']."=".$val           
                    ) ;
            }
        }