cancel
Showing results for 
Search instead for 
Did you mean: 

create module to add new Category in magento 2.2

create module to add new Category in magento 2.2

Hello,

I want to create module in magento 2.2 that when I run it , it will create new Category  in admin panel  catalog/category

2 REPLIES 2

Re: create module to add new Category in magento 2.2

Hi @danial_safaripour

 

okay - understand !! so here you required two things to achieve your requirements.

 

1st - you need to create a custom module - might be you know how to create a custom module still i am sharing the link how to create a custom module - https://www.magestore.com/magento-2-tutorial/how-to-create-a-module-in-magento-2/

 

2nd  - important point is second one - here you need to create custom category programmatically - so in your custom module you need to add below code :

 

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');
/// 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','Basics'); // 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());
$mediaAttribute = array ('image', 'small_image', 'thumbnail');
$categoryTmp->setImage('/m2.png', $mediaAttribute, true, false);// Path pub/meida/catalog/category/m2.png
$categoryTmp->setStoreId($storeId);
$categoryTmp->setPath($rootCat->getPath());
$categoryTmp->save();
}

Here i am sharing link how to create a category progrmatically in your custom module - http://www.webetutorial.com/blog/magento-2-add-category-programmatically/

 

https://blog.mdnsolutions.com/magento-2-add-category-programmatically/

 

https://magento.stackexchange.com/questions/134911/magento-2-create-categories-programmatically

 

 

so at the end - first create a custom module and then write a code for create a category programatically - then link your custom module controller/router to menu or some where and run everytime your custom module URL - it will generate a category in admin for you !

 

Hope it helps to you !!

if issue solved,Click Kudos & Accept as Solution

Re: create module to add new Category in magento 2.2

where i put this code ?? Can you Explain !