1

How can I associate simple products to a configurable product programmatically?

1

Assuming you know the product ids and the simple products have values for the configurable attribute:

$configurable_product = Mage::getModel('catalog/product')->load($id_of_configurable);
$configurable_product->getTypeInstance()
                     ->saveProducts(
                         $configurable_product, 
                         $array_of_simple_product_ids
                     );
0

The below code create associated products pro-grammatically. The code create products with variable attributes.

 foreach ($simpleProductSkus as $simpleProdSku) {

        $i = 0;
        echo 'simple sku: '.$simpleProdSku . PHP_EOL;
        $simple_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $simpleProdSku);
        if (is_object($simple_product)) {

            if ((int)$simple_product->getSize()) {
                $productsData = array( //['920'] = id of a simple product associated with this configurable
                    'label' => $simple_product->getAttributeText('size'), //attribute label
                    'attribute_id' => (int)$sizeAttributeId, //attribute ID of attribute 'color' in my store
                    'value_index' => (int)$simple_product->getSize(), //value of 'Green' index of the attribute 'color'
                    'is_percent' => '0', //fixed/percent price for this option
                    'pricing_value' => $simple_product->getPrice() //value for the pricing
                );
                $configurableProductsData[$simple_product->getId()] = $productsData;
                $configurableAttributesData[$i]['values'][] = $productsData;
                $i++;
            }
             if ((int)$simple_product->getColor()) {
                $productsData = array(
                    'label' => $simple_product->getAttributeText('color'),
                    'attribute_id' => (int)$colorAttributeId,
                    'value_index' => (int)$simple_product->getColor(),
                    'is_percent' => '0',
                    'pricing_value' => $simple_product->getPrice()
                );
                $configurableProductsData[$simple_product->getId()] = $productsData;
                $configurableAttributesData[$i]['values'][] = $productsData;
                $i++;
            }

             if ((int)$simple_product->getLength()) {
                $productsData = array( //['920'] = id of a simple product associated with this configurable
                    'label' => $simple_product->getAttributeText('length'), //attribute label
                    'attribute_id' => (int)$lengthAttributeId, //attribute ID of attribute 'color' in my store
                    'value_index' => (int)$simple_product->getLength(), //value of 'Green' index of the attribute 'color'
                    'is_percent' => '0',
                    'pricing_value' => $simple_product->getPrice() //value for the pricing
                );
                $configurableProductsData[$simple_product->getId()] = $productsData;
                $configurableAttributesData[$i]['values'][] = $productsData;
                $i++;
            }
            if ((int)$simple_product->getMaterial()) {
                $productsData = array(
                    'label' => $simple_product->getAttributeText('material'),
                    'attribute_id' => (int)$materialAttributeId,
                    'value_index' => (int)$simple_product->getMaterial(),
                    'is_percent' => '0',
                    'pricing_value' => $simple_product->getPrice()
                );
                $configurableProductsData[$simple_product->getId()] = $productsData;
                $configurableAttributesData[$i]['values'][] = $productsData;
                $i++;
            }

            if ((int)$simple_product->getWeightspec()) {
                $productsData = array( //['920'] = id of a simple product associated with this configurable
                    'label' => $simple_product->getAttributeText('weightspec'), //attribute label
                    'attribute_id' => (int)$weightspecAttributeId, //attribute ID of attribute 'color' in my store
                    'value_index' => (int)$simple_product->getWeightspec(), //value of 'Green' index of the attribute 'color'
                    'is_percent' => '0',
                    'pricing_value' => $simple_product->getPrice() //value for the pricing
                );
                $configurableProductsData[$simple_product->getId()] = $productsData;
                $configurableAttributesData[$i]['values'][] = $productsData;
                $i++;
            }

        }
    }

Please refer the tutorial for detailed explanation. http://www.pearlbells.co.uk/set-associated-products-magento-1/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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