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.