How to get all Root Categories ids in Magento 2?

If your Website is setup with multiple store and each store has different Root Category assigned, In that case you need to refer below blog for getting all root category ids by programmatically.

Let’s consider your store with Two Root Category available.

First Native Magento Default Category which id is 2.
Second, create custom Root Category based on your requirement which id is 42.

Now How to get above root category id using coding in Magento 2, You can get root category id by below way,

<?php
namespace Rbj\Root\Block;
 
class RootCategoryIds extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * Return ids of root categories as array
     *
     * @return array
     */
    public function getRootIds()
    {
        $ids = [\Magento\Catalog\Model\Category::TREE_ROOT_ID];
        foreach ($this->_storeManager->getGroups() as $store) {
            $ids[] = $store->getRootCategoryId();
        }
        return $ids;
    }

Call from template file,

echo "<pre>";print_r($this->getRootIds());

Output as array,

Array
(
    [0] => 1
    [1] => 2
    [2] => 42
)

In above case 1 is for Root Catalog id,
2 is your Default Category id.
42 is your custom created root category id.

Let’s explore get category collection per store by,
Get Category Collection of specific store

 

How to get default website id in Magento 2?

For Multi website setup of Magento e-commerce site, Sometimes need to get Default Website id using programmatically, You need to refer below code snippet for getting default website id.

Hope, You are aware of how to get website collection and other details using coding.
Create Block file,

<?php
namespace Rbj\Website\Block;

class GetWebsite extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * @return string|int
     */
    public function getDefaultWebsiteId()
    {
        return $this->_storeManager->getDefaultStoreView()->getWebsiteId();
    }

Call from template file or PHP file,

echo $this->getDefaultWebsiteId(); // output is your default website code.
For single website output is 1.

How to get website collection programmatically in Magento 2?

Magento 2 is robust, scalable and strong functional e-commerce software used for online shopping store. Its support Multi website, multi-language,multi-store view for an online store. Many times in custom functionality for Multi website store need to get a collection of all the website information. In this case below code is useful.

Here We will go for how to get all website collection of the system by programmatically.

Create Block file,

<?php
namespace Rbj\Website\Block;

class WebsiteList extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\ResourceModel\Website\CollectionFactory $websiteCollectionFactory,
        array $data = []
    ) {
        $this->_websiteCollectionFactory = $websiteCollectionFactory;
        parent::__construct($context, $data);
    }

     /**
     * Retrieve websites collection of system
     *
     * @return Website Collection
     */
    public function getWebsiteCollection()
    {
        $collection = $this->_websiteCollectionFactory->create();
        return $collection;
    }

Call from template file,

<?php
foreach($block->getWebsiteCollection() as $website) {
	echo $website->getCode(); //website code
	echo $website->getName(); //website name
	echo "<pre>";print_r($website->getData());
}

To get all the data output look like,

Array
(
    [website_id] => 1
    [code] => base
    [name] => Main Website
    [sort_order] => 0
    [default_group_id] => 1
    [is_default] => 1
)