Hello Devs,
In today’s Quick tutorial, we will see how to get all websites lists programmatically in Magento 2.
Default Magento provides one Magento website, store, and store views.
This information is usually stored in the “store_website” table of your Magento 2 database.
Magento provides WebsiteRepositoryInterface to fetch all store lists and website-related information :
Magento\Store\Api\WebsiteRepositoryInterface
Using this repository we can fetch website-related information like website_id, code, name, default_group_id, and is_default.
See example below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace Jigar\Tutorials\Block; use Magento\Store\Api\WebsiteRepositoryInterface; /** * Class for get all websites list. */ class Example { /** * @var WebsiteRepositoryInterface */ private $websiteRepository; /** * @param WebsiteRepositoryInterface $websiteRepository */ public function __construct( WebsiteRepositoryInterface $websiteRepository ) { $this->websiteRepository = $websiteRepository; } /** * {@inheritdoc} */ public function getAllWebsitesList() { $websites = []; foreach ($this->websiteRepository->getList() as $website) { $websites[] = $website->getData(); } return $websites; } } |
As shown in the above example,
when we call the getAllWebsitesList() function, it will return all website lists with website-related information.
Sample Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[ [ "website_id" => "0", "code" => "admin", "name" => "Admin", "sort_order" => "0", "default_group_id" => "0", "is_default" => "0", ], [ "website_id" => "1", "code" => "base", "name" => "Main Website", "sort_order" => "0", "default_group_id" => "1", "is_default" => "1", ], ] |
You can change the function logic as per your requirement.
That’s it for this tutorial, Do not miss to check below useful tutorials below.
Magento 2: How to get all stores list programmatically
how to get shared catalog data by ID in Magento 2?
How to Get Store Configuration value in Magento 2?
Thank You!!