3

May I know how to do categories import in Magento 2? I did not see the option in "System>Import" or in the "Products>Categories" section of the backend.

It is really puzzling the best Ecommerce CMS in the world such as magento does not even permit user to import categories.

I have 500+ categories but I don't want to type them one by one.

Please help :)

2 Answers 2

3

Magento 2 does not have a separate categories import, but you can have categories created when importing products. You need to add a "categories" column to your products csv file that lists the categories the product should be placed in. By default, multiple categories are separated by a comma. Sub-categories are indicated by a slash.

So a categories entry like:
"Mens/Pants/Jeans, Mens/Pants/Casual"

would create these categories when the products where imported.

  • Mens
    • Pants
      • Jeans
      • Casual

If you aren't sure how to format the Products CSV file, create a few entries in the admin interface and then export the products to get a sample file.

2
  • Beth , thank you so much for your response :) Does this mean there will be no way for me to import category description, category meta , and category URL ? Prestashop has a complete category import fields in a separate import process than magento. It's just their SEO perform so poorly. Apr 24, 2016 at 17:07
  • I've never tried importing those fields and I don't know of a way to do it. You might want to check on marketplace.magento.com to see if someone has created a module to do that import. Apr 25, 2016 at 13:30
0

Please use the following script to bulk import categories magento 2.

 foreach ( $arrResult as $import_category ) {

    try {

        $enabled = ( strtolower( $import_category[16] ) == 'true' ) ? 1 : 0;
        $parentId = ( $import_category[1] == 0 )? '2' : $list[$import_category[1]];            

        $url = strtolower($import_category[2]);
        $cleanurl = trim(preg_replace('/ +/', '', preg_replace('/[^A-Za-z0-9 ]/', '', urldecode(html_entity_decode(strip_tags($url))))));
        $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
        /// Add a new sub category under root category
        $category = $categoryFactory->create();
        $category->setName(ucfirst($import_category[2]));
        $category->setIsActive( $enabled );
        $category->setUrlKey($cleanurl);
        $category->setData('description', strip_tags($import_category[11]));
        $category->setParentId($parentId);
        $mediaAttribute = array ('image', 'small_image', 'thumbnail');
        $category->setImage( $import_category[6], $mediaAttribute, true, false);// Path pub/meida/catalog/category/
        $category->setStoreId($storeId);
        $rootCat = $objectManager->get('Magento\Catalog\Model\Category')->load($parentId);
        $category->setPath($rootCat->getPath());
        $category->save();
        $list[$import_category[0]] = $category->getId();
        echo 'Category ' . $category->getName() . ' ' . $category->getId() . ' imported successfully' . PHP_EOL;
    } 
    catch (Exception $e) 
    {
        echo 'Something failed for category ' . $import_category[2] . PHP_EOL;
        print_r($e);
    }

Click here for detailed explanation . http://www.pearlbells.co.uk/import-magento-2-categories-programmatically/

1
  • I have try this code but not working for me. Jun 20, 2019 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.