1
votes

In my store only configurable products are visible.simple products are hidden. each configurable product has associated simple products.

I want to filter configurable product by child product attribute.

For example if configurable product child has color = blue that product should load in product collection

1

1 Answers

0
votes
$attributeCode = 'color';
$attributeValue = 'Blue';

$attribute = Mage::getSingleton('eav/config')
                ->getAttribute('catalog_product', $attributeCode);

if ($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
}

$attributeValueId = 0;
foreach ($options as $option) {
    if ($option['label'] == $attributeValue) {
        $attributeValueId = $option['value'];
    }
}

$productId = YOUR_CONFIGURABLE_PRODUCT_ID; // ID of configurable product

$product = Mage::getModel('catalog/product')->load($productId);

$childProducts = Mage::getModel('catalog/product_type_configurable')                            
                    ->getUsedProductCollection($product)
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter($attributeCode, $attributeValueId);

Source: Magento: Filter Configurable Product by Child Product’s Attribute