Magento is a robust, scalable online platform used for selling online. Such an eCommerce platform supports the multi-website, multi-language and multi-store view. So, it is possible to build multiple websites, their stores and store view depending on the business requirements.
Are you a Magento 2 store admin handling multiple websites on the same platform?
When running an online store with multiple websites sometimes you may need to get the list of all the websites in Magento 2. Then, this solution will help you to programmatically get all websites in Magento 2.
Method to Programmatically Get All Websites in Magento 2:
The method to get the list of all the websites in Magento 2 is as follows:
use Magento\Store\Model\ResourceModel\Website\CollectionFactory as WebsiteCollectionFactory; protected $websiteCollectionFactory; public function __construct(WebsiteCollectionFactory $websiteCollectionFactory) { $this->websiteCollectionFactory = $websiteCollectionFactory; } //get website collection $websiteCollection = $this->websiteCollectionFactory->create();
That’s it.
Also, do not forget to share the solution with the Magento Community via social media.
Thank you.