Import associated configurable products

This tutorial I am explaining how to import associated products magento.

function saveSingleProducts($import_product,$sku,$enabled,$optionsArray,$visible,$migrated_categories,$dir) {

     try {
         
        $attrLabel = 'color';        
        $product = Mage::getModel('catalog/product');
        $attr = $product->getResource()->getAttribute($attrLabel);
        $color_id = $attr->getSource()->getOptionId($optionsArray['value']);

        $productName =  $import_product[1].' '.$optionsArray['value'];
        $skuSingle = $sku.$optionsArray['value'];
        $product->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
                ->setAttributeSetId(4)  //ID of a attribute set 
                ->setTypeId('simple') //product type
                ->setCreatedAt(strtotime('now')) //product creation time
                ->setSku($skuSingle) //SKU
                ->setName($productName) //product name
                ->setWeight($import_product[16])
                ->setStatus($enabled) //product status (1 - enabled, 2 - disabled)
                ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
                ->setVisibility($visible)//catalog and search visibility
                ->setColor($color_id)
                ->setPrice($import_product[11])//price in form 11.22
                ->setCost(1)//price in form 11.22              
                ->setMetaTitle($import_product[1])
                ->setMetaKeyword($import_product[26])
                ->setMetaDescription($import_product[28])
                ->setDescription($import_product[27])
                ->setShortDescription($import_product[27])
                ->setStockData(array(
                        'use_config_manage_stock' => 1, //'Use config settings' checkbox
                        'is_in_stock' => 1, //Stock Availability
                        'qty' => (int) $optionsArray['qty'] //qty
                    )
                )
                ->setCategoryIds( $migrated_categories); //assign product to categories
        
        if ($product->save()){ //($product->save()) {
                echo 'Child Product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
            }
            
        } catch (Exception $e) {
            Mage::log($e->getMessage());
        }
        return  $product;
}

Share this Tutorial