$categories = array(
array(
'name' => 'Homedfd Appliances fsdfsdfd',
'parent' => 97,
'child' => array(
'Television',
'AC & Coolers',
'Refrigerator',
'Microwave Oven',
'Fans', 'Kitchen Appliances',
'Other Appliances' )
),
array(
'name' => 'Compudfdter dsfd',
'parent' => 97,
'child' => array(
'Monitor',
'Hard Drive',
'Printer',
'Wireless & Networking',
'Webcams',
'Keyboard',
'Mouse'
)
),
array(
'name' => 'Groomsfding Products',
'parent' => 97,
'child' => array(
'Trimmers & Shavers',
'Hair Styling Tools'
)
),
array(
'name' => 'Moddbile & Tabs',
'parent' => 97,
'child' => array()
)
);
/** Calling to magento functionalities*/use \Magento\Framework\App\Bootstrap;include('./app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$mediaurl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$websiteId = $storeManager->getWebsite()->getWebsiteId();
$store = $storeManager->getStore();
$storeId = $store->getStoreId();
$rootNodeId = $store->getRootCategoryId();
$categoryModel = $objectManager->get('Magento\Catalog\Model\Category');
$rootCategoryInfo = $categoryModel->load($rootNodeId);
$outputFile = fopen('catCreateResult.txt', 'w');
foreach($categories as $cat){
$name=ucfirst($cat['name']);
$url=strtolower($cat['name']);
$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);
/* * If you want to include category description please ucomment below line
* As well as include a key with description value in category array and provide it on here.
*/
//$categoryTmp->setData('description', 'description');
$categoryTmp->setParentId($cat['parent']);
$mediaAttribute = array ('image', 'small_image', 'thumbnail');
/* * You can also set an category image. */
//$categoryTmp->setImage('/m2.png', $mediaAttribute, true, false);
// Path pub/meida/catalog/category/m2.png $categoryTmp->setStoreId($storeId);
$parentCategoryInfo = $categoryModel->load($cat['parent']);
$categoryTmp->setPath($parentCategoryInfo->getPath());
$output = $categoryTmp->save();
$newId = $output->getEntityId();
if(count($cat['child']) > 0){
foreach($cat['child'] as $sub_child){
$name=ucfirst($sub_child);
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryTmp = $categoryFactory->create();
$categoryTmp->setName($name);
$categoryTmp->setIsActive(true);
$categoryTmp->setParentId($newId);
$mediaAttribute = array ('image', 'small_image', 'thumbnail');
//$categoryTmp->setImage('/m2.png', $mediaAttribute, true, false); // Path pub/meida/catalog/category/m2.png
$categoryTmp->setStoreId($storeId);
$parentCategoryInfo = $categoryModel->load($newId);
$categoryTmp->setPath($parentCategoryInfo->getPath());
$categoryTmp->save();
}
}
}
For More Description you may visit here.