How to Programmatically Get All Websites in Magento 2
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:
1 2 3 4 5 6 7 8 9 10 11 |
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.
Any doubts? If so, do mention them in the comments section below. I’d be happy to help.
Also, do not forget to share the solution with the Magento Community via social media.
Thank you.
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
New Magento 2 Extensions, Updates & Services Launches [February 2022]
Difference Between Website, Store & Store View in Magento 2
Next