Import Configurable products

function saveConfigureProducts($import_product,$sku,$enabled,$migrated_categories, $simple_products,$discount_arr,$dir) { try { $tierPrices = array(); $product = Mage::getModel(‘catalog/product’); $visible = Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH; $product->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array ->setAttributeSetId(4) //ID of a attribute set named ‘Colours’ ->setTypeId(‘configurable’) //product type ->setCreatedAt(strtotime(‘now’)) //product creation time ->setSku($sku) //SKU ->setName($import_product[1]) //product name ->setWeight($import_product[16]) ->setStatus($enabled) //product status (1 – enabled, 2 –… Read more »

function saveConfigureProducts($import_product,$sku,$enabled,$migrated_categories, $simple_products,$discount_arr,$dir) {

     try {
         $tierPrices = array();
        $product = Mage::getModel('catalog/product');
        $visible = Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH;
        $product->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
                ->setAttributeSetId(4)  //ID of a attribute set named 'Colours'
                ->setTypeId('configurable') //product type
                ->setCreatedAt(strtotime('now')) //product creation time
                ->setSku($sku) //SKU
                ->setName($import_product[1]) //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
                ->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(!empty($import_product[28]) ? $import_product[28] : 'Default Meta Description')
                ->setDescription(!empty($import_product[27]) ? $import_product[27] : 'Default Description')
                ->setShortDescription(!empty($import_product[27]) ? $import_product[27] : 'Default Description')
                ->setStockData(array(
                        'use_config_manage_stock' => 0, //'Use config settings' checkbox
                        'manage_stock' => 1, //manage stock
                        'is_in_stock' => 1, //Stock Availability
                    )
                )
                ->setCategoryIds($migrated_categories); //assign product to categories


         /**/
         /** assigning associated product to configurable */
         /**/
         $colorAttributeId = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', 'color');
         $product->getTypeInstance()->setUsedProductAttributeIds(array($colorAttributeId)); //attribute ID of attribute 'color' in my store
         $configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();

         $product->setCanSaveConfigurableAttributes(true);
         $product->setConfigurableAttributesData($configurableAttributesData);

         $configurableProductsData = array();
         foreach ($simple_products as $simple_product){
             $configurableProductsData[$simple_product->getId()] = array( //['920'] = id of a simple product associated with this configurable
                     'label' => $simple_product->getAttributeText($attrLabel),
                     'attribute_id' => (int)$colorAttributeId, //attribute ID of attribute 'color' in my store
                     'value_index' => (int) $simple_product->getColor(), //value of 'Green' index of the attribute 'color'
                     'is_percent' => '0', //fixed/percent price for this option
                     'pricing_value' => $product->getPrice() //value for the pricing
             );
         }
         $product->setConfigurableProductsData($configurableProductsData);
        
       
            

        if ($product->save()){ //($product->save()) {
            echo 'Configurable Product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
        } else {
            echo 'ERROR: Product Didnt Save'.$import_product[0];
        }

        //Mage::getResourceSingleton('catalog/product_type_configurable')->saveProducts($product, $simple_product_ids);

        } catch (Exception $e) {
            Mage::log($e->getMessage());
        }
        
        return array($import_product[0],$product->getId(), $import_product[33]);

}

Share this Tutorial