Skip to content

Instantly share code, notes, and snippets.

@SpencerRohan
Forked from 0-Sony/WebsiteAndStoreCreator.php
Last active January 11, 2022 02:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpencerRohan/839933a564bbecfc6949af71dd1b1dfe to your computer and use it in GitHub Desktop.
Save SpencerRohan/839933a564bbecfc6949af71dd1b1dfe to your computer and use it in GitHub Desktop.
Magento 2 : Create Programmatically Website/Store/StoreGroup
<?php
namespace XX\StoreBuilder\Setup;
/**
* Class InstallData
* @package XX\StoreBuilder\Setup
*/
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\ResourceModel\Group;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Store\Model\ResourceModel\Website;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\WebsiteFactory;
class InstallData implements InstallDataInterface
{
/**
* @var ManagerInterface
*/
private $eventManager;
/**
* @var GroupFactory
*/
private $groupFactory;
/**
* @var Group
*/
private $groupResourceModel;
/**
* @var StoreFactory
*/
private $storeFactory;
/**
* @var Store
*/
private $storeResourceModel;
/**
* @var WebsiteFactory
*/
private $websiteFactory;
/**
* @var Website
*/
private $websiteResourceModel;
/**
* InstallData constructor.
* @param WebsiteFactory $websiteFactory
* @param Website $websiteResourceModel
* @param Store $storeResourceModel
* @param Group $groupResourceModel
* @param StoreFactory $storeFactory
* @param GroupFactory $groupFactory
* @param ManagerInterface $eventManager
*/
public function __construct(
Group $groupResourceModel,
GroupFactory $groupFactory,
ManagerInterface $eventManager,
Store $storeResourceModel,
StoreFactory $storeFactory,
Website $websiteResourceModel,
WebsiteFactory $websiteFactory
) {
$this->eventManager = $eventManager;
$this->groupFactory = $groupFactory;
$this->groupResourceModel = $groupResourceModel;
$this->storeFactory = $storeFactory;
$this->storeResourceModel = $storeResourceModel;
$this->websiteFactory = $websiteFactory;
$this->websiteResourceModel = $websiteResourceModel;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$default_attributes = [
[
'website_code' => 'sitea',
'website_name' => 'Site A',
'group_name' => 'Site A Online Store',
'store_code' => 'site_a_online_store_view',
'store_name' => 'Site A Store View',
'is_active' => '1'
],
[
'website_code' => 'siteb',
'website_name' => 'Site B',
'group_name' => 'Site B Online Store',
'store_code' => 'site_b_online_store_view',
'store_name' => 'Site B Store View',
'is_active' => '1'
],
];
foreach ($default_attributes as $attribute)
{
/** @var \Magento\Store\Model\Store $store */
$store = $this->storeFactory->create();
$store->load($attribute['store_code']);
if(!$store->getId()){
/** @var \Magento\Store\Model\Website $website */
$website = $this->websiteFactory->create();
$website->load($attribute['website_code']);
$website = $this->setWebID($website, $attribute);
/** @var \Magento\Store\Model\Group $group */
$group = $this->groupFactory->create();
$group->setWebsiteId($website->getWebsiteId());
$group->setName($attribute['group_name']);
$this->groupResourceModel->save($group);
$group = $this->groupFactory->create();
$group->load($attribute['group_name'], 'name');
$store->setCode($attribute['store_code']);
$store->setName($attribute['store_name']);
$store->setWebsite($website);
$store->setGroupId($group->getId());
$store->setData('is_active', $attribute['is_active']);
$this->storeResourceModel->save($store);
$this->eventManager->dispatch('store_add', ['store' => $store]);
$store = $this->storeFactory->create();
}
}
$setup->endSetup();
}
/**
* @param \Magento\Store\Model\Website $website
* @param array $attribute
* @return \Magento\Store\Model\Website
*/
public function setWebID($website, $attribute)
{
if(!$website->getId()){
$website->setCode($attribute['website_code']);
$website->setName($attribute['website_name']);
$this->websiteResourceModel->save($website);
}
return $website;
}
}
@EviNoita
Copy link

EviNoita commented Aug 20, 2018

After the 123 line, add $group->setCode($attribute['store_code']);
When creating the first group of the group, the code = null attribute when creating the second group, we get an error - "Group with the same code already exists".

You can also immediately specify RootCategoryId, this can be specified in an array or the default category id = 2.
$group->setRootCategoryId($attribute['root_category_id']);

@luisfxnada
Copy link

luisfxnada commented Nov 19, 2018

I have been trying to run this script, but I can't seem to get it working. Would you be kind to give some details on usage? Such as how to run it, where to place it, etc.

Thank you much in advance!

@johnorourke
Copy link

Don't inject the resource models like this, it causes an object caching problem where creating the website causes an exception, because the inventory module's afterSave hook fails because it can't find the newly created website.

Instead use $website->getResource()->save($website) and $store->getResource()->save($store) and it'll work like a charm.

@0-Sony
Copy link

0-Sony commented Jan 11, 2019

Don't inject the resource models like this, it causes an object caching problem where creating the website causes an exception, because the inventory module's afterSave hook fails because it can't find the newly created website.

Instead use $website->getResource()->save($website) and $store->getResource()->save($store) and it'll work like a charm.

I don't know with which Magento version you are working, but from 2.2.6 i notice that $entity->getResource()->save($entity) is deprecated. When i want to save a entity, I use resourceModel and save method with my entity as param.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment