Question

Here is the code I am using but it is given error. Please help. Thanks in advance.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$rootCat = $objectManager->create('Magento\Catalog\Model\Category');
$cat_info = $rootCat->load(2);
$catName = "Tool Inventory";
$category = $objectManager->get('\Magento\Catalog\Model\CategoryFactory')->create();
$store = $this->storeManager->getStore();
$storeId = $store->getStoreId();
$url=strtolower($catName);
$cleanurl = trim(preg_replace('/ +/', '', preg_replace('/[^A-Za-z0-9 ]/', '', urldecode(html_entity_decode(strip_tags($url))))));
$category->setName($catName);
$category->setIsActive(true);
$category->setUrlKey($cleanurl);
$category->setData('description', 'description');
$category->setParentId($rootCat->getId());
$category->setStoreId($storeId);
$category->setPath($rootCat->getPath());
$objectManager->get('\Magento\Catalog\Api\CategoryRepositoryInterface')->save($category);

enter image description here

Was it helpful?

Solution

Try below code in your controller:

<?php

namespace {Vendor}\{Module}\Controller\{ModuleFolderName};

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

class CreateCategory extends Action
{
    
    protected $category;
    protected $categoryFactory;
    protected $CategoryRepository;
    protected $storeManager;

    public function __construct(
        Context $context,
        Category $category,
        CategoryFactory $categoryFactory,
        CategoryRepositoryInterface $CategoryRepository,
        StoreManagerInterface $storeManager

    ) {
        parent::__construct($context);
        $this->category = $category;
        $this->categoryFactory = $categoryFactory;
        $this->CategoryRepository = $CategoryRepository;
        $this->storeManager = $storeManager;
    }

    
    public function execute()
    {
        echo "Start!!!";

        $cat_info = $this->category->load(2);
        $catName = "Some name";
        $category = $this->categoryFactory->create();

        $storeId = $this->storeManager->getStore()->getStoreId();

        $url=strtolower($catName);
        $cleanurl = trim(preg_replace('/ +/', '', preg_replace('/[^A-Za-z0-9 ]/', '', urldecode(html_entity_decode(strip_tags($url))))));
        $category->setName($catName);
        $category->setIsActive(true);
        $category->setUrlKey($cleanurl);
        $category->setData('description', 'description new');
        $category->setParentId($this->category->getId());
        $category->setStoreId($storeId);
        $category->setPath($this->category->getPath());
        $this->CategoryRepository->save($category);

        echo "End!!";
    }
}

You can also try in root script by following code:

<?php
use Magento\Framework\App\Bootstrap;
 
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


$objectManager = $bootstrap->getObjectManager();


$rootCat = $objectManager->create('Magento\Catalog\Model\Category');
$cat_info = $rootCat->load(2);
$catName = "Tool Inventory";
$category = $objectManager->get('\Magento\Catalog\Model\CategoryFactory')->create();

$store  = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeId = $store->getStore()->getStoreId();

$url=strtolower($catName);
$cleanurl = trim(preg_replace('/ +/', '', preg_replace('/[^A-Za-z0-9 ]/', '', urldecode(html_entity_decode(strip_tags($url))))));
$category->setName($catName);
$category->setIsActive(true);
$category->setUrlKey($cleanurl);
$category->setData('description', 'description');
$category->setParentId($rootCat->getId());
$category->setStoreId($storeId);
$category->setPath($rootCat->getPath());
$objectManager->get('\Magento\Catalog\Api\CategoryRepositoryInterface')->save($category);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top