Magento category import csv

Magento category import csv

September 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento category import csv

Magento Category Import from CSV: A Comprehensive Guide

Hello developers,

I hope this post finds you well! Today, I’m going to dive into a common challenge faced by many of us working with Magento—importing categories from a CSV file that contains both parent and child IDs.

The Challenge at Hand

You’ve likely seen data structures like the following:

PARENTID|ID|NAME
1|3|ITEM 1
1|4|ITEM 2
4|5|CHILD 1
5|8|SUB-CHILD 1

The goal is to import this into Magento, creating a hierarchical structure that looks like this:

ITEM 1 (id=3)
ITEM 2 (id=4)
   - CHILD 1(id=5)
      - SUB-CHILD 1(id=8)

Understanding the Magento Category Structure

Before we begin, it’s crucial to understand how categories are structured in Magento. Each category has a parent_id field that references its parent category.

Important: In Magento, categories form a tree structure with no cycles allowed. This means you cannot have a self-referencing or circular structure.

Mapping the CSV Data to Magento

To import this data into Magento, we’ll use the Magento’s built-in import functionality along with some custom scripting.

First, let me share an example of how you might map your CSV data to a format suitable for importing:

<?php
$csv = fopen('categories.csv', 'r');

// Skip header row
fgetcsv($csv);

while (($row = fgetcsv($csv)) !== false) {
    $parentId = (int)$row[0];
    $id = (int)$row[1];
    $name = trim($row[2]);

    // Construct the category data array
    $categoryData[] = [
        'entity_id' => null,
        'attribute_set_code' => 4, // Set this to your desired attribute set
        'parent_id' => $parentId,
        'created_at' => now(),
        'updated_at' => now(),
        'name' => $name,
    ];
}

fclose($csv);

Preparing the Import File

Next, we need to prepare a file that Magento can import. Magento expects the data in a specific format.

Important: Magento uses a fixed-width format for importing categories. Each field must be a fixed width and padded with spaces if necessary.

You can create this file using PHP or any programming language of your choice. Here’s an example:

<?php

$categories = [
    // ... (your category data array from the previous step)
];

// Define the fixed-width format for each field
$format = "%10s%10s%s";

// Open the output file
$file = fopen('output.csv', 'w');

// Write the header row to the file
fputs($file, "entity_id parent_id name attribute_set_code updated_at created_at\n");

// Loop through the categories and write each one as a line in the file
foreach ($categories as $category) {
    fputcsv($file, array_map(function ($value) use ($format) {
        return sprintf($format, str_pad((string)$value, 10));
    }, $category));
}

fclose($file);

Importing the Categories

Now that you have your CSV file ready, it’s time to import it into Magento.

Important: Always backup your data before performing an import. It’s better to be safe than sorry!

To import the categories:

  1. Navigate to System > Import/Export.
  2. In the Data Flow section, click on Categories.
  3. Click on New Upload File.
  4. Choose your file and select the correct delimiter (usually a comma).
  5. Map the columns in the CSV file to the corresponding fields in Magento.
  6. Click Check Data to validate the import data.
  7. If everything looks good, click Import.

Wrapping Up

And there you have it! With some careful mapping and preparation of your CSV data, you can easily import category hierarchies into Magento.

Remember, always test your imports on a staging environment before deploying them to production. This helps catch any potential issues before they affect live traffic.

I hope this guide was helpful! If you need further assistance or have questions about Magento development, feel free to reach out or check out the comprehensive resources at Laravel Company.

Happy coding!

Best, [Your Name]

Catalog Database Import/Export

Our network