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
)

 

How to get customer group collection in Magento 2?

In Magento 2 We can easily get customer group programmatically by using below code snippets. You are aware of How to create customer and new address programmatically in Magento 2

Below code snippets display all the customer group in the system,
Call groupCollectionFactory using __construct() in block file,

<?php
namespace Rbj\CustomerGroup\Block;

class CustomerGroup extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory,
        array $data = []
    ) {
        $this->groupCollectionFactory = $groupCollectionFactory;
        parent::__construct($context, $data);
    }

    /**
     * Retrieve customer group collection
     *
     * @return GroupCollection
     */
    public function getCustomerGroupCollection()
    {
        if (!$this->hasData('customer_group_collection')) {
            $collection = $this->groupCollectionFactory->create();
            $this->setData('customer_group_collection', $collection);
        }

        return $this->getData('customer_group_collection');
    }

Call from template file,

<?php
$customerGroupCollection = $block->getCustomerGroupCollection();
foreach($customerGroupCollection as $customerGroup) {
	echo 'ID '.$customerGroup->getId();
	echo 'Code '.$customerGroup->getCode();
	echo 'Tax class Id '.$customerGroup->getTaxClassId();
	echo "<br>";
}

The output will be look like,

ID 0 Code NOT LOGGED IN Tax class Id 3
ID 1 Code General Tax class Id 3
ID 2 Code Wholesale Tax class Id 3
ID 3 Code Retailer Tax class Id 3