0

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"

2 Answers 2

2

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           
                    ) ;
            }
        }
1
  • thanks works. although trimValue() errors. Also would be nice to get the frontend option_title (in our case this is size, where we expect to see S, M, Large etc ... instead of a code 51, 31, 22). thanks
    – snh_nl
    Jun 6, 2018 at 6:54
0

Do like this :

<?php
    $productId = 1; //config product id
    $product = Mage::getModel('catalog/product')->load($productId);
    $configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
    $simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();


    foreach($simpleCollection as $simple){
       //$simple->getName();
        if($simple->getSize() == 'medium'){
            // Do whatever you want
        }
    }
?>

Your Answer

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

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