How to create custom script for import products in magento

How to create custom script for import products in magento

July 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to create custom script for import products in magento

Custom Product Import Script for Magento 1.8

As a senior developer working extensively with e-commerce platforms like Magento, I often encounter scenarios where clients need to import products from custom CSV files containing unique fields that don’t match the default product attributes. In this post, we’ll guide you through creating a custom script to handle such imports effectively in Magento 1.8.

Prerequisites

Before diving into code, ensure your Magento installation is up-to-date and running on PHP version 5.3 or higher. This tutorial assumes basic familiarity with Magento’s architecture and the command line.

Step-by-Step Guide: Creating a Custom Import Script

1. Define Your CSV Format

Firstly, create a custom CSV file with columns that match your product requirements. For instance:

sku,product_name,product_description,price,weight
SKU123,Product A,This is a sample product,9.99,0.5
SKU456,Product B,A different product description,19.99,1.2

2. Create the Import Script

Navigate to your Magento root directory via SSH and create a new file in the shell folder:

touch custom_import.php

Open this file using your preferred text editor or IDE.

3. Write the Import Logic

Here’s an outline of what your script should contain, using Magento’s built-in import classes:

#!/usr/bin/php -f
<?php

require_once 'abstract.php';

class CustomImport extends Mage_Shell_Abstract
{
    public function run()
    {
        $file = 'path_to_your/import.csv';
        $adapter = new Varien_File_Csv();
        $data = $adapter->getData($file);

        foreach ($data as $row) {
            $product = Mage::getModel('catalog/product');

            // Map your custom fields to Magento product attributes
            $product->setSku($row[0]);
            $product->setName($row[1]);
            $product->setDescription($row[2]);
            $product->setPrice($row[3]);

            try {
                $product->save();
            } catch (Exception $e) {
                Mage::logException($e);
            }
        }

        echo "Import complete.";
    }
}

Don’t forget to replace 'path_to_your/import.csv' with the actual path on your server.

4. Run the Script

Save the file and run it from the command line:

php shell/custom_import.php

Best Practices for Custom Imports

  1. Use transactional logic: Wrap your import process within a try-catch block to ensure data integrity in case of errors.

  2. Clean up temporary files: After successful imports, delete the CSV file to prevent accidental duplicates or conflicts.

  3. Optimize performance: For large-scale imports, consider using a database transaction for faster processing times. You can refer to our guide on Magento performance optimization for more insights.

Conclusion

Creating a custom import script in Magento 1.8 requires understanding the platform’s architecture and leveraging its built-in classes effectively. By following this tutorial, you’ve learned how to:

  • Define a custom CSV structure
  • Create a PHP script using Magento’s shell functionality
  • Map custom fields to core product attributes
  • Handle errors gracefully during the import process

Remember that every project is unique, so feel free to modify and extend this basic script according to your specific needs. Always test scripts thoroughly before running them on live environments.

Happy coding! If you have any further questions or need assistance with complex integrations, don’t hesitate to reach out to our team at https://laravelcompany.com.

Magento 1 Catalog Pricing Import/Export

Our network