Connect with us

Blog

Magento 2: Add category programmatically

mm

Published

on

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;

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

//$state->setAreaCode(‘frontend’);

// 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){

$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(‘/category.png’, $mediaAttribute, true, false);// Path pub/meida/catalog/category/category.png

$categoryTmp->setStoreId($storeId);

$categoryTmp->setPath($rootCat->getPath());

$categoryTmp->save();

}

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

Also Read :   Mandana Karimi’s seductive photoshoot

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
Advertisement
Click to comment

Leave a Reply

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

9 − 9 =

Blog

Burj Al Arab Jumeirah – Luxurious Hotel in the World

mm

Published

on

Burj Al Arab Jumeirah – Luxurious Hotel in the World

Burj Al Arab Jumeirah is a world best luxurious hotel with their famous ultra-luxurious suites overlooking the sea. It is own island with nine signature restaurant. It has an opulent full-service spa and most attractive thing which guests may arrive to see the world’s largest chauffeur-driven fleets of Rolls-Royce’s.

The point which make luxurious to this property:

  • The roof terrace offers large swimming pools, 32 luxury cabanas same like a resort.
  • Floor have panoramic view of the Arabian Gulf
  • Each suite includes an iPad, WiFi, iMac and HD TV.
  • The hotel Bar is floating 200 m above sea level.
  • Guest may prefer to see sea in afternoon with tea and cocktails.
  • Talise Spa and ladies & gentlemen’s relaxation areas feature an aqua retreat.
  • It also includes separate indoor infinity pools facilities, hot tub and treatment room’s facilities, steam rooms and plunge pools facilities.
  • The property suite have private beach for special guests.
  • The property offers water sports activities at Wild Wadi Waterpark™.
Also Read :   WordPress Create Own Forget Password Page

Quick view on Property Amenities:

  • Pool
  • Room service
  • Restaurant
  • Free High Speed Internet (WiFi)
  • Fitness Centre with Gym / Workout Room
  • Free parking
  • Spa
  • Bar/Lounge
  • Adult pool
  • Airport transportation
  • Babysitting
  • Banquet Room
  • Breakfast Available
  • Business Centre with Internet Access
  • Children Activities (Kid / Family Friendly)
  • Concierge
  • Conference Facilities
  • Dry Cleaning
  • Heated pool
  • Hot Tub
  • Indoor pool
  • Laundry Service
  • Meeting rooms
  • Multilingual Staff
  • Non-smoking hotel
  • Outdoor pool
  • Sauna
  • Wheelchair Access

Quick view on Rooms:

  • Air conditioning
  • Minibar
  • Refrigerator in room
  • Accessible rooms
  • Family Rooms
  • Non-smoking rooms
  • Smoking rooms available
  • Suites

In the Dubai guests are getting more for their money when compared with other properties in the same location.

Subscribe us to get more hotels & resorts news or want to advertise your hostel with us, inquire now to get best quote from us.

Continue Reading

Trending