Set associated products magento 1

This tutorial explains how to set associated products magento 1 programmatically. The program imports products with five different attributes (length, size, material, color and weightspec) and set associated products programmatically.

Click here to download the sample CSV. The products are imported from the sample CSV file. There are some products with one, other with two and three attributes.

set_associated_products_magento_1
set_associated_products_magento

Step by step explanation

  1. Get Attribute Ids
  2. Identify associated attributes
  3. Set Associated Products Magento 1

Get Attribute Ids

As the first get the attributes Ids.

get_attribute_ids_magento_1

Identify associated attributes

The smaple

Identify_associated_products_attributes_magento_1

Set Associated Products Magento 1

function setAssociatedProducts( $product, $simpleProdSkus ) {

    try {

       $configurableProductsData = array();
       // get Attribute Ids
       $colorAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'color');
       $sizeAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'size');
       $lengthAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'length');
       $materialAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'material');
       $weightspecAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'weightspec');

        $simpleProductSkus = explode(',',$simpleProdSkus);
        
        // Idenfity the attributes associated with the child products

        $attrExist = array();
        $firstChildproduct =  Mage::getModel('catalog/product')->loadByAttribute('sku', $simpleProductSkus[0]);

        $i = 0;
        if ((int)$firstChildproduct->getSize()) {
            $attrExist[0] = $sizeAttributeId;
            $i++;
        }
        if ((int)$firstChildproduct->getColor()) {
            $attrExist[1] = $colorAttributeId;
            $i++;
       }

        if ((int)$firstChildproduct->getLength()) {
            $attrExist[2] = $lengthAttributeId;
            $i++;
        }
        if ((int)$firstChildproduct->getMaterial()) {
            $attrExist[3] = $materialAttributeId;
            $i++;
        }
        if ((int)$firstChildproduct->getWeightspec()) {
            $attrExist[4] = $weightspecAttributeId;
            $i++;
        }
        
        $product->getTypeInstance()->setUsedProductAttributeIds($attrExist);
        $configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();

        // Assign child products to the configurable products
        
       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 'size' in my store
                        'value_index' => (int)$simple_product->getSize(), //value of '3mm' index of the attribute 'size'
                        '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, /
                        'value_index' => (int)$simple_product->getLength(), 
                        '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, 
                        'value_index' => (int)$simple_product->getWeightspec(), 
                        'is_percent' => '0',
                        'pricing_value' => $simple_product->getPrice() //value for the pricing
                    );
                    $configurableProductsData[$simple_product->getId()] = $productsData;
                    $configurableAttributesData[$i]['values'][] = $productsData;
                    $i++;
                }
               
            }
        }

        $product->setConfigurableProductsData($configurableProductsData);
        $product->setConfigurableAttributesData($configurableAttributesData);
        $product->setCanSaveConfigurableAttributes(true);
        //Mage::log($configurableProductsData,null,'product.log');
        //Mage::log($configurableAttributesData,null,'attr.log');

    } catch (Exception $ex) {
        Mage::log($ex->getMessage());

    }
    return $product;

}

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*