10 use Magento\Catalog\Model\CategoryFactory;
58 private $websiteIds = [];
63 private $storeGroupsIds = [];
68 private $storeGroupsToWebsites = [];
73 private $storeManager;
83 private $defaultStoreGroup;
88 private $defaultWebsite;
93 private $defaultParentCategoryId;
98 private $defaultStoreGroupId;
103 private $defaultWebsiteId;
108 private $storeGroupsCount;
113 private $storesCount;
118 private $websitesCount;
123 private $singleRootCategory;
128 private $defaultStoreView;
133 private $storeViewIds;
138 private $eventManager;
143 private $categoryFactory;
148 private $localeConfig;
163 CategoryFactory $categoryFactory,
169 $this->eventManager = $eventManager;
170 $this->categoryFactory = $categoryFactory;
172 $this->scopeConfig = $scopeConfig;
182 $this->websitesCount = $this->fixtureModel->getValue(
'websites', self::DEFAULT_WEBSITE_COUNT);
183 $this->storeGroupsCount = $this->fixtureModel->getValue(
'store_groups', self::DEFAULT_STORE_COUNT);
184 $this->storesCount = $this->fixtureModel->getValue(
'store_views', self::DEFAULT_STORE_VIEW_COUNT);
185 $this->singleRootCategory = (bool)$this->fixtureModel->getValue(
'assign_entities_to_all_websites',
false);
187 if ($this->websitesCount <= self::DEFAULT_WEBSITE_COUNT
188 && $this->storeGroupsCount <= self::DEFAULT_STORE_COUNT
189 && $this->storesCount <= self::DEFAULT_STORE_VIEW_COUNT
195 $storeGroups = $this->storeManager->getGroups();
196 $this->storeGroupsIds= array_keys($storeGroups);
202 $this->websiteIds = array_values(array_unique($this->storeGroupsToWebsites));
204 $this->defaultWebsite = $this->storeManager->getWebsite();
205 $this->defaultStoreGroup = $this->storeManager->getGroup();
206 $this->defaultWebsiteId = $this->defaultWebsite->getId();
207 $this->defaultStoreGroupId = $this->defaultStoreGroup->getId();
208 $this->defaultStoreView = $this->storeManager->getDefaultStoreView();
209 $this->storeViewIds = array_keys($this->storeManager->getStores());
211 $this->generateWebsites();
212 $this->generateStoreGroups();
213 $this->generateStoreViews();
220 private function generateWebsites()
224 while ($existedWebsitesCount <= $this->websitesCount) {
225 $website = clone $this->defaultWebsite;
226 $websiteCode = sprintf(
'website_%d', $existedWebsitesCount);
227 $websiteName = sprintf(
'Website %d', $existedWebsitesCount);
230 'website_id' =>
null,
232 'name' => $websiteName,
233 'is_default' =>
false,
237 $this->websiteIds[] =
$website->getId();
238 $existedWebsitesCount++;
246 private function generateStoreGroups()
248 $existedStoreGroupCount = count($this->storeGroupsIds);
249 $existedWebsitesCount = count($this->websiteIds);
251 while ($existedStoreGroupCount < $this->storeGroupsCount) {
252 $websiteId = $this->websiteIds[$existedStoreGroupCount % $existedWebsitesCount];
253 $storeGroupName = sprintf(
'Store Group %d - website_id_%d', ++$existedStoreGroupCount,
$websiteId);
254 $storeGroupCode = sprintf(
'store_group_%d', $existedStoreGroupCount);
261 'name' => $storeGroupName,
262 'code' => $storeGroupCode,
263 'root_category_id' => $this->getStoreCategoryId($storeGroupName),
276 private function generateStoreViews()
278 $localesList = $this->localeConfig->getAllowedLocales();
279 $localesListCount = count($localesList);
281 $existedStoreViewsCount = count($this->storeViewIds);
282 $existedStoreGroupCount = count($this->storeGroupsIds);
284 while ($existedStoreViewsCount < $this->storesCount) {
285 $groupId = $this->storeGroupsIds[$existedStoreViewsCount % $existedStoreGroupCount];
287 $store = clone $this->defaultStoreView;
288 $storeCode = sprintf(
'store_view_%d', ++$existedStoreViewsCount);
290 'Store view %d - website_id_%d - group_id_%d',
291 $existedStoreViewsCount,
306 $this->saveStoreLocale(
$store->getId(), $localesList[$existedStoreViewsCount % $localesListCount]);
315 private function saveStoreLocale(
$storeId, $localeCode)
317 $this->scopeConfig->save(
318 \
Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE,
331 private function getStoreCategoryId($storeGroupName)
333 if ($this->singleRootCategory) {
334 return $this->getDefaultCategoryId();
337 $category = $this->categoryFactory->create();
339 $category->setName(
"Category " . $storeGroupName)
340 ->setPath($categoryPath)
342 ->setAvailableSortBy(
'name')
343 ->setDefaultSortBy(
'name')
356 return 'Generating websites, stores and store views';
365 'websites' =>
'Websites',
366 'store_groups' =>
'Store Groups Count',
367 'store_views' =>
'Store Views Count' 376 private function getDefaultCategoryId()
378 if (
null === $this->defaultParentCategoryId) {
379 $this->defaultParentCategoryId = $this->storeManager->getStore()->getRootCategoryId();
381 return $this->defaultParentCategoryId;
const DEFAULT_WEBSITE_COUNT
const DEFAULT_STORE_COUNT
const DEFAULT_STORE_VIEW_COUNT
if(!isset($_GET['website_code'])) $websiteCode
__construct(FixtureModel $fixtureModel, StoreManager $storeManager, ManagerInterface $eventManager, CategoryFactory $categoryFactory, Config $localeConfig, Writer $scopeConfig)