Connect with us


Magento 2: Add category programmatically




In this article, I am going to show, how to create multiple categories programmatically. It is useful to import number of categories data from API, CSV and XML file.

Here is a code:

use \Magento\Framework\App\Bootstrap;


$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’);   // if you need state then uncomment below line of code


// Get Website ID

$websiteId = $storeManager->getWebsite()->getWebsiteId();

// Get Store ID$store = $storeManager->getStore();

$storeId = $store->getStoreId();

// Get Root Category ID, here you can also add you current Category Id as Root Id to import child category

$rootNodeId = $store->getRootCategoryId();

// Get Root Category

$rootCat = $objectManager->get(‘Magento\Catalog\Model\Category’);

$cat_info = $rootCat->load($rootNodeId);

$categorys=array(‘Car’,’Bike’,’Bus’);// Category Names

foreach($categorys as $cat){



$cleanurl = trim(preg_replace(‘/ +/’, ”, preg_replace(‘/[^A-Za-z0-9 ]/’, ”, urldecode(html_entity_decode(strip_tags($url))))));


// Add a new sub category under root category

$categoryTmp = $categoryFactory->create();$categoryTmp->setName($name);


$categoryTmp->setData(‘description’, ‘description’);


$mediaAttribute = array (‘image’, ‘small_image’, ‘thumbnail’);

$categoryTmp->setImage(‘/category.png’, $mediaAttribute, true, false);// Path pub/meida/catalog/category/category.png





Now should be able to see it in your Back-end, Products > Categories.

Also Read :   How to Use Magento 2 Layouts, Blocks and Templates

Hi, Thank you for reading my article. I am Victor, a professional blogger from Jaipur, India.I started webetutorial for blogging & sharing solution for developer questions, and now it’s empowering globally by helping them to make money.I am writing about starting & managing a blogs, WordPress, Magento, Social Media, SEO, Marketing, making money online, Investment, Finance, Gadgets, Fitness and more.

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

seventeen − 5 =


Ways to Increase Comments on Your Blog




Ways to Increase Comments on Your Blog

Everyone knows commenting on the other website blogs give the benefits of backlinks, traffic, and branding of our linked website and we are trying lots of things to create a good backlinks which would give a huge converting traffic to us. Sometime people commenting a those website which doesn’t have good reputation or not a much traffic so, their efforts are not useful for them.

Commenting is the ways to drive traffic that is facebook commenting, twitter commenting or website commenting. Here we are going to tell you about some points which may give you huge traffic to increase comments on your blog.

Blog Comment Points Are:

Get visitors to subscribe: Many readers don’t want to comment at first time on your blog because they don’t have much truest on your blog and they don’t know about how much the traffic on your blog getting. To ask them to subscribe to your blog for more blog or webinar this will help to increase comments, traffic and revenue.

Also Read :   Four Credit Cards Thick LG 4K OLED TV

Disable spam prevention methods: If you are using captcha, then you need to remove them so user can comment without any interruption.

Avoid forcing registration: If you are using registration process for commenting on your site ten remove them because this create far too much resistance and makes it much more irritate for your visitors to comment.

Link to your comments section at the end of your post: Lot of blog themes have a problem with its comment section, visitors have to scroll the whole page at the bottom for the commenting. Your blog should have comment link at the top of page with blog title where visitor can click and move directly to comment section.

Go back and think about the audience you’re trying to reach: If you are getting many comments then try to give answer of your visitors which give your attention to your visitor and help you to reach your goal and focus on the right areas.

Also Read :   How to Use Magento 2 Layouts, Blocks and Templates

Leave an unanswered question at the end of your post: Ask a question at the end of blog which is the one of the best ways to get readers attention to comment on your blog. Your aim should to develop a discussion within your comments and the more discussion.

Respond to comments and encourage more conversations: As we talk in Go back and think about the audience you’re trying to reach engaging your audience for getting more comments in general ways to respond to comments for further conversation.

You can let us your point of views in the comment or subscribe us to get more blog from us.

Continue Reading