1

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

1

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);
2
  • Thank You @pawan sir for help. I found the issue, it is not related to my code. The issue is due to Rubix theme DB structure. – Shubham Sahu May 9 at 15:26
  • glad to know @ShubhamSahu that you found solution... – Pawan May 9 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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