Import configurable products magento

This tutorial explains how to import configurable products programmatically using magento scripts.Here first it import the child products of the configurable products.

The products are importing based on the following CSV .Please see the previous tutorial which explains how to read data from CSV.

Click here to download the products.csv and options.csv

In options.csv it contains two options black and white corresponding to the product id 1.So the product with product id 1 is a configurable product.We are importing the products as shown below.

import-configurable-products-magento

Step by step explanation

  1. Import child products
  2. Import configurable products magento
  3. Assign associated product to the configurable products

Import child products

This function imports the associated products of the configurable products.

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;
}

Import configurable products magento

This function create the configurable product and assigned the associated products to it.

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]);

}

import-categories-magento

Share this Tutorial

Leave a Reply

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

*