Categories are essential for creating an efficient catalog and customer navigation so that users can easily find the products they want on your website.

Each product should be assigned to at least one category. Categories are a helpful way to sort and arrange your products. The diagram below represents the structure of the category.

Magento 2 categories with subcategories

The Magento 2 Import/Export function is one of the best resolutions for all stores to simplify numerous tasks in the manual back-end. These features become more essential and useful for Magento merchants.

Migrate To Magento 2

Does Default Magento 2 Open Source Support Import Categories?

The answer is definitely, no. In default Magento 2, the admin creates categories one by one, complete with the related data in each category. In the case of migrating to Magento 2, or when updating or moving to a new website, manually creating categories would be such a time-consuming work that not every business would tolerate it.

The below script helps you to import each category with its subcategories simultaneously without the compulsory need of the parent category ID. Instead of that, we can use the parent category name.

Create a custom module as shown in the file structure below.

Note: Please take a backup of your data and check with your technical developer before running the code.

Magento 2 categories with subcategories

Step 1: Create 




Step 2: Create a form to import a CSV.




Step 3: Create a controller to read and import the CSV.



Run this module:


Magento 2 categories with subcategories

The CSV format must be as shown below.

Click here to get a sample CSV.

In this CSV, your header will be what you want and the first column should be a serial number. The second column must be either a parent/root category ID or name, and the third column must be the name of the category.


************************ Import CSV File ************************

Sample CSV Format

S.No Parent Category ID/Name Category
1 2 Mobile
2 Mobile Samsung
3 Mobile Oppo
4 Mobile Vivo
5 Samsung Samsung J Pro
6 Samsung Samsung a 10
7 Oppo Oppo Youth
8 Oppo Oppo k1
9 Vivo Vivo v5 plus
10 Vivo Vivo v9

Magento 2 categories with subcategories

And finally, you can now simultaneously import each category with its subcategories. If you found this blog helpful, then please do let us know if you have any queries.


  • Vijayashanthi M

    Vijayashanthi M is a passionate coder with an experience of 2.5 years in PHP, Yii 2, and Magento 2. She is always on the lookout for interesting things on the internet, and is an avid learner of new technologies. Her hobbies include dancing and listening to music.


Ready to drive online revenue

Get In Touch

One thought on “Magento 2: Import a category with its subcategories programmatically

  1. How many category levels we can add them here, It is possible to have only two levels or more than 2 levels also we can add. Please help me here, I want to import the categories which are nearly 750+, I want to import them into my website. I am expecting an answer from you. Thank you.

Leave a Reply

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