0

I am creating a category programmatically in magento 2 in controller, but I am getting error Call to undefined method Magento\Catalog\Model\ResourceModel\Category\Flat::getEntityTable() I have also run indexing to solve a problem but no luck.

Note: I have enabled flat categories in configuration.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $url = \Magento\Framework\App\ObjectManager::getInstance();
            $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
            //$state = $objectManager->get('\Magento\Framework\App\State');
            //$state->setAreaCode('frontend');
            /// Get Website ID
            $websiteId = $storeManager->getWebsite()->getWebsiteId();
            echo 'websiteId: '.$websiteId." ";
    
            /// Get Store ID
            $store = $storeManager->getStore();
            $storeId = $store->getStoreId();
            echo 'storeId: '.$storeId." ";
    
            /// Get Root Category ID
            $rootNodeId = $store->getRootCategoryId();
            echo 'rootNodeId: '.$rootNodeId." ";
            /// Get Root Category
            $rootCat = $objectManager->get('Magento\Catalog\Model\Category');
            $cat_info = $rootCat->load($rootNodeId);
    
            $categorys=array('Levis','Wranglers'); // Category Names
            foreach($categorys as $cat)
            {
    
            $name=ucfirst($cat);
            $url=strtolower($cat);
            $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
            $categoryTmp = $categoryFactory->create();
            $categoryTmp->setName($name);
            $categoryTmp->setIsActive(true);
            $categoryTmp->setUrlKey($cleanurl);
            $categoryTmp->setData('description', 'description');
            $categoryTmp->setParentId($rootCat->getId());
            $categoryTmp->setStoreId($storeId);
            $categoryTmp->setPath($rootCat->getPath());
            $categoryTmp->save();
            }
7
  • Try $categoryFactory=$objectManager->create('\Magento\Catalog\Model\CategoryFactory'); Commented Dec 29, 2020 at 5:08
  • @HamendraSunthwal If I used your code it throws Error: Call to undefined method Magento\Catalog\Model\CategoryFactory::setName() Commented Dec 29, 2020 at 5:21
  • You need to load a category using id & then you will be able to use set methods. Like $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');// Instance of Category Model $categoryId = 15; // YOUR CATEGORY ID $category = $categoryFactory->create()->load($categoryId); Commented Dec 29, 2020 at 7:01
  • My guess you may want to emulate an admin area.. that is probably the issue. I might take a look at it tomorrow depending how the day goes Commented Dec 30, 2020 at 21:37
  • @HerveTribouilloy ok thanks I set as adminarea while creating category but it throws error "it already set area". Thank you for your message Commented Dec 30, 2020 at 21:44

0

Your Answer

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