Assign associated products magento 1

This tutorial explains how to import child products magento 1 and set associated products programmatically. Here it 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 variable attributes.

The below code the creates the child products and set the configurable products attributes as shown below.

set_associated_products_magento_1
set_associated_products_magento

Step by step explanation

  • Get Attribute Ids
  • Identify associated products attributes
  • Set Associated Products Magento 1

Get Attribute Ids

The following code get all the attribute ids.

get_attribute_ids_magento_1

Identify associated products attributes

We are importing the prodcuts with variable attributes. As per the CSV we have products with one attribute and some products with two or three attributes. The step we need to determine the attributes associated with the child products.

Identify_associated_products_attributes_magento_1

Import child products magento 1

The below code set the associated products as show above.

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