-
-
Save cagartner/6cad1e401c69b8bc47c56cc892c5a5dd to your computer and use it in GitHub Desktop.
Magento 2 : Create Programmatically Website/Store/StoreGroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Example\Store\Setup; | |
use Magento\Framework\App\Area; | |
use Magento\Framework\App\State; | |
use Magento\Framework\Config\ScopeInterface; | |
use Magento\Framework\Event\ManagerInterface; | |
use Magento\Store\Model\Group; | |
use Magento\Store\Model\GroupFactory; | |
use Magento\Store\Model\ResourceModel\Group as GroupResource; | |
use Magento\Store\Model\ResourceModel\Store as StoreResource; | |
use Magento\Store\Model\ResourceModel\Website as WebsiteResource; | |
use Magento\Store\Model\Store; | |
use Magento\Store\Model\StoreFactory; | |
use Magento\Store\Model\Website; | |
use Magento\Store\Model\WebsiteFactory; | |
/** | |
* Class StoreCreator | |
* @package Example\Store\Setup | |
*/ | |
class StoreCreator | |
{ | |
/** | |
* @var WebsiteResource | |
*/ | |
private $websiteResourceModel; | |
/** | |
* @var WebsiteFactory | |
*/ | |
private $websiteFactory; | |
/** | |
* @var GroupResource | |
*/ | |
private $groupResourceModel; | |
/** | |
* @var GroupFactory | |
*/ | |
private $groupFactory; | |
/** | |
* @var StoreResource | |
*/ | |
private $storeResourceModel; | |
/** | |
* @var StoreFactory | |
*/ | |
private $storeFactory; | |
/** | |
* @var ManagerInterface | |
*/ | |
private $eventManager; | |
/** | |
* @var State | |
*/ | |
private $appState; | |
/** | |
* @var ScopeInterface | |
*/ | |
private $scope; | |
/** | |
* StoreCreator constructor. | |
* @param WebsiteResource $websiteResourceModel | |
* @param WebsiteFactory $websiteFactory | |
* @param GroupResource $groupResourceModel | |
* @param GroupFactory $groupFactory | |
* @param StoreResource $storeResourceModel | |
* @param StoreFactory $storeFactory | |
* @param ManagerInterface $eventManager | |
* @param State $appState | |
* @param ScopeInterface $scope | |
*/ | |
public function __construct( | |
WebsiteResource $websiteResourceModel, | |
WebsiteFactory $websiteFactory, | |
GroupResource $groupResourceModel, | |
GroupFactory $groupFactory, | |
StoreResource $storeResourceModel, | |
StoreFactory $storeFactory, | |
ManagerInterface $eventManager, | |
State $appState, | |
ScopeInterface $scope | |
) | |
{ | |
$this->websiteResourceModel = $websiteResourceModel; | |
$this->websiteFactory = $websiteFactory; | |
$this->groupResourceModel = $groupResourceModel; | |
$this->groupFactory = $groupFactory; | |
$this->storeResourceModel = $storeResourceModel; | |
$this->storeFactory = $storeFactory; | |
$this->eventManager = $eventManager; | |
$this->appState = $appState; | |
$this->scope = $scope; | |
} | |
/** | |
* @param array $websites | |
* @throws \Exception | |
*/ | |
public function create(array $websites) | |
{ | |
// $websites = [ | |
// [ | |
// 'website_code' => 'example_a', | |
// 'website_name' => 'Example Website A', | |
// 'groups' => [ | |
// [ | |
// 'group_code' => 'example_a_a', | |
// 'group_name' => 'Example Group A-A', | |
// 'root_category_id' => 2, | |
// 'stores' => [ | |
// [ | |
// 'store_code' => 'example_a_a_a', | |
// 'store_name' => 'Example Store A-A-A', | |
// 'is_active' => 1, | |
// ], | |
// [ | |
// 'store_code' => 'example_a_a_b', | |
// 'store_name' => 'Example Store A-A-B', | |
// 'is_active' => 1, | |
// ], | |
// ] | |
// ], | |
// [ | |
// 'group_code' => 'example_a_b', | |
// 'group_name' => 'Example Group A-B', | |
// 'root_category_id' => 2, | |
// 'stores' => [ | |
// [ | |
// 'store_code' => 'example_a_b_a', | |
// 'store_name' => 'Example Store A-B-A', | |
// 'is_active' => 1, | |
// ], | |
// ] | |
// ], | |
// ], | |
// ], | |
// [ | |
// 'website_code' => 'example_b', | |
// 'website_name' => 'Example Website B', | |
// 'groups' => [ | |
// [ | |
// 'group_code' => 'example_group_b_a', | |
// 'group_name' => 'Example Group B-A', | |
// 'root_category_id' => 2, | |
// 'stores' => [ | |
// [ | |
// 'store_code' => 'example_store_b_a_a', | |
// 'store_name' => 'Example Store B-A-A', | |
// 'is_active' => 1, | |
// ], | |
// ] | |
// ], | |
// ], | |
// ], | |
// ]; | |
$currentScope = $this->scope->getCurrentScope(); | |
$this->appState->emulateAreaCode(Area::AREA_ADMINHTML, function() use ($websites) { | |
$this->scope->setCurrentScope(Area::AREA_ADMINHTML); | |
foreach ($websites as $websiteData) { | |
/** @var Website $website */ | |
$website = $this->websiteFactory->create(); | |
$website->load($websiteData['website_code']); | |
if (!$website->getId()) { | |
$website = $this->setAttributeToWebsite( | |
$website, | |
$websiteData | |
); | |
$this->websiteResourceModel->save($website); | |
} | |
if ($website->getId()) { | |
foreach ($websiteData['groups'] as $groupData) { | |
/** @var Group $group */ | |
$group = $this->groupFactory->create(); | |
foreach ($website->getGroupCollection() as $item) { | |
if ($groupData['group_code'] == $item->getCode()) { | |
$group = $item; | |
break; | |
} | |
} | |
if (!$group->getId() && !empty($groupData['group_code'])) { | |
$group = $this->setAttributeToGroup( | |
$group, | |
$groupData, | |
$website | |
); | |
$this->groupResourceModel->save($group); | |
} | |
if ($group->getId()) { | |
foreach ($groupData['stores'] as $storeData) { | |
/** @var Store $store */ | |
$store = $this->storeFactory->create(); | |
foreach ($group->getStoreCollection() as $item) { | |
if ($storeData['store_code'] == $item->getCode()) { | |
$store = $item; | |
break; | |
} | |
} | |
if (!$store->getId() && !empty($storeData['store_code'])) { | |
$store = $this->setAttributeToStore( | |
$store, | |
$storeData, | |
$website, | |
$group | |
); | |
$this->storeResourceModel->save($store); | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
$this->scope->setCurrentScope($currentScope); | |
} | |
/** | |
* @param Website $website | |
* @param array $data | |
* @return Website | |
* @throws \Magento\Framework\Exception\AlreadyExistsException | |
*/ | |
private function setAttributeToWebsite(Website $website, array $data) | |
{ | |
$website->setCode($data['website_code']); | |
$website->setName($data['website_name']); | |
return $website; | |
} | |
/** | |
* @param Group $group | |
* @param array $data | |
* @param Website $website | |
* @return Group | |
* @throws \Magento\Framework\Exception\AlreadyExistsException | |
*/ | |
private function setAttributeToGroup(Group $group, array $data, Website $website) | |
{ | |
$group->setWebsiteId($website->getWebsiteId()); | |
$group->setCode($data['group_code']); | |
$group->setName($data['group_name']); | |
$group->setRootCategoryId($data['root_category_id']); | |
return $group; | |
} | |
/** | |
* @param Store $store | |
* @param array $data | |
* @param Website $website | |
* @param Group $group | |
* @return Store | |
* @throws \Magento\Framework\Exception\AlreadyExistsException | |
*/ | |
private function setAttributeToStore(Store $store, array $data, Website $website, Group $group) | |
{ | |
$store->setCode($data['store_code']); | |
$store->setName($data['store_name']); | |
$store->setWebsite($website); | |
$store->setGroupId($group->getId()); | |
$store->setData('is_active', $data['is_active']); | |
return $store; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment