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.

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 

app/code/DCKAP/CategoryImport/view/frontend/layout/categoryimport_index_index.xml

 

 

Step 2: Create a form to import a CSV.

app/code/DCKAP/CategoryImport/view/frontend/templates/categoryimport.phtml

 

 

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

app/code/DCKAP/CategoryImport/Controller/Index/Index.php

 

Run this module:

http://localhost/magento/categoryimport/

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.

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote

Leave a Reply

Be the First to Comment!