Import magento products

The tutorial import magento products.Please go through the main tutorial which import the categories and attributes and also reads the data from the products.csv file.This section explains how to import the products.

The importproducts() function iterate through the array and creates the simple and configurable products.The options.csv file contains the color options corresponding to each products. Based on that it decides whether to create simple or configurable products.

Download the products.csv and options.csv sample csv files from here. In the products.csv file contain two product with productid 1 and 2 . But in the options.csv file there is only one product with (productid =1) have 2 options (black and red). So the product with id 1 is configurable product and the second product is the simple product.

The following figure shows the $arrResult content :

import products magento

The importproducts() function accept the following parameters.
$arrResult – This array contains all the product data
$options_arr – This array contains all the data from the options.csv
$dir – Image directory
$new_cats – New category ids
$discount_arr – Tier price details

Import configurable products

In this function it iterate through the products array and check whether the options array corresponding to that product is set. If it set it means that product is a configurable product.Then saveSingleProducts() function create the assoicated products of the configurable product.

Import simple products

If the options array is not set then it creates the simple products.

function importProducts($arrResult,  $options_arr, $dir, $new_cats,$discount_arr)
{

    $dummy_sku = 'PKSKU';
    $dummy_sku_counter = 0;
    $new_product_related = array();


    foreach ($arrResult as $import_product) {

        $migrated_categories = getMigratedCategory($import_product[2], $new_cats);

        if ($import_product[3] == '' || $import_product[3] == null) {
            $sku = $dummy_sku . $dummy_sku_counter++;
        } else {
            $sku = $import_product[3] . $dummy_sku_counter++;
        }

        if (strtolower($import_product[22]) == true) { //status
            $enabled = 1;
        } else {
            $enabled = 2;
        }
        if(isset($options_arr[$import_product[0]]))
        {
            $visible = Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE;
            $simple_products = array();

            foreach($options_arr[$import_product[0]] as $optionsArray) {
               $simple_products[] = saveSingleProducts($import_product,$sku,$enabled,$optionsArray,$visible,$migrated_categories,$dir);
            }
           
            $new_product_related[$import_product[0]] = saveConfigureProducts($import_product,$sku,$enabled,$migrated_categories, $simple_products,$discount_arr,$dir);
        }            
        else
        {
            $visible = Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH;
            $new_product_related[$import_product[0]] = saveSingleProduct($import_product,$sku,$enabled,$visible,$migrated_categories,$discount_arr,$dir);
        }
    }  
//        $related_id[] = array
        return  $new_product_related;
            
   
}
  1. Import Simple Products – click here
  2. Import Configurable Products – click here

Share this Tutorial

Leave a Reply

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

*