In this article, I’m going to show how to create categories programmatically. It comes in handy when you are trying to import data from APIs, CSV files, XML documents and so on.

Also see: Magento 2: Delete all categories

Firstly, you need to load your data into an array. We will insert the categories one by one using the methods below (I will explain those methods next):

foreach ($array_with_categories as $row) {
    $data = $this->prepareData($row);
    $this->createCategory($data);
}

As you can see above, I prepare the data to then create the category afterwards. I personally find it an easier way to manipulate data and it’s quite re-usable.

    /**
     * @param string $row
     * @param int $id
     * @return array mixed
     */
    protected function prepareData($row, $id = null)
    {
          // in case you want to user the root category id
//        $rootCat = $this->_objectManager->get('Magento\Catalog\Model\Category');
//        $cat_info = $rootCat->load($rootrowId);
//        $categoryTmp->setPath($rootCat->getPath());

        $data = [
            'data' => [
                "parent_id" => 2,
                'name' => $row['category_name'],
                "is_active" => true,
                "position" => 10,
                "include_in_menu" => false,
            ],
            'custom_attributes' => [
                "display_mode"=> "PRODUCTS",
                "is_anchor"=> "1",
//                "custom_use_parent_settings"=> "0",
//                "custom_apply_to_products"=> "0",
//                "url_key"=> "", // if not set magento uses the name to generate it
//                "url_path"=> "cat2",
//                "automatic_sorting"=> "0",
//                'my_own_attribute' => 'value' // <-- your attribute
            ]
        ];

        if($id) {
            $data['data']['id'] = $id;
        }

        return $data;
    }

Once the data is ready, let’s create the new category using the returned data above.

/**
 * @param array $data
 * @return bool
 */
protected function createCategory(array $data)
{
    $category = $this->_objectManager
        ->create('Magento\Catalog\Model\Category', $data)
        ->setCustomAttributes($data['custom_attributes']);

    $repository = $this->_objectManager->get(CategoryRepositoryInterface::class);
    $result = $repository->save($category);

    echo "Created Category " . $data['data']['name'] . "\n";

    return true;
}

Now should be able to see it in your Back-end, Products > Categories.

Happy coding!

https://i1.wp.com/blog.mdnsolutions.com/wp-content/uploads/2016/05/Screen-Shot-2016-05-06-at-9.29.33-AM.png?fit=452%2C240&ssl=1https://i1.wp.com/blog.mdnsolutions.com/wp-content/uploads/2016/05/Screen-Shot-2016-05-06-at-9.29.33-AM.png?resize=150%2C150&ssl=1Renato MedinaMagentoMagento 2Category,Magento 2,ProgrammaticallyIn this article, I'm going to show how to create categories programmatically. It comes in handy when you are trying to import data from APIs, CSV files, XML documents and so on. Also see: Magento 2: Delete all categories Firstly, you need to load your data into an array. We will insert the categories one by...A Magento Craftsman in Melbourne