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 )