0

I would like to write some code to create a new category under root category (as shown below). The new category has been created at database, but I cannot found it on admin page

    /// Get Web Site ID
    $websiteId = $this->_storeManager->getWebsite()->getWebsiteId();
    echo $websiteId."/n/r";

    /// Get Store ID
    $store = $this->_storeManager->getStore();
    $storeId = $store->getStoreId();
    echo $storeId."/n/r";

    /// Get Root Category ID
    $rootNodeId = $store->getRootCategoryId();
    echo $rootNodeId."/n/r";

    /// Add a new sub category under root category
    $categoryTmp = $this->_categoryFactory->create();
    $categoryTmp->setName('test');
    $categoryTmp->setIsActive(true);
    $categoryTmp->setUrlKey('test5');
    $categoryTmp->setData('description', 'description');
    $categoryTmp->setParentId($rootNodeId);
    $categoryTmp->setStoreId($storeId);
    $categoryTmp->save();
1
  • My guess problem is reindex so try redindex once
    – Magento 2
    Feb 12 '16 at 4:45
2

I think should incorrect in setPath. I've amended my code, it work now! thanks.

    /// Get Web Site ID
    $websiteId = $this->_storeManager->getWebsite()->getWebsiteId();
    echo 'websiteId: '.$websiteId."   ";

    /// Get Store ID
    $store = $this->_storeManager->getStore();
    $storeId = $store->getStoreId();
    echo 'storeId: '.$storeId."   ";

    /// Get Root Category ID
    $rootNodeId = $store->getRootCategoryId();
    echo 'rootNodeId: '.$rootNodeId."   ";

    /// Get Root Category
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $rootCat = $objectManager->get('Magento\Catalog\Model\Category');
    $cat_info = $rootCat->load($rootNodeId);

    /// Add a new sub category under root category
    $categoryTmp = $this->_categoryFactory->create();
    $categoryTmp->setName('test2');
    $categoryTmp->setIsActive(true);
    $categoryTmp->setUrlKey('test14');
    $categoryTmp->setData('description', 'description');
    $categoryTmp->setParentId($rootNodeId);
    $categoryTmp->setStoreId($storeId);
    $categoryTmp->setPath($rootCat->getPath());
    $categoryTmp->save();
0

it will help for Programmatically created a new category

$parent_cat_id = \Magento\Catalog\Model\Category::TREE_ROOT_ID;

$parent_category = $this->_objectManager
                      ->create('Magento\Catalog\Model\Category')
                      ->load($parent_cat_id);
$category_obj = $this->_objectManager
                ->create('Magento\Catalog\Model\Category');
// Check category exist or not
$cate_data = $category_obj->getCollection()
            ->addAttributeToFilter('name','CATE_NAME')
            ->getFirstItem();

if(!isset($cate_data->getId())) 
{
    $category_obj->setPath($parent_category->getPath())
        ->setParentId($parent_cat_id)
        ->setName('CATE_NAME')
        ->setIsActive(true);
    $category_obj->save();
}
2
  • With above code, when flat_category enabled, not creating new categories. When I use $this->_objectManager ->create('Magento\Catalog\Model\CategoryRepository')->save($categoryObj); it is creating new category but not inside default category i.e parentId=2, it is not taking. It is creating category inside root only. Can someone resolve? Sep 8 '17 at 10:25
  • Another issue.. With Flat_category enabled , new categories not getting created as flat model is missing validate() function. Anyone has solution? Sep 9 '17 at 8:17

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.