Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommyready/73a5dc88dc1cfa56d99de06497e6b9ee to your computer and use it in GitHub Desktop.
Save tommyready/73a5dc88dc1cfa56d99de06497e6b9ee to your computer and use it in GitHub Desktop.
Magento 2 : Create Programmatically Website/Store/StoreGroup
<?php
/**
* This file is part of Namespace for Magento.
*
* @license All rights reserved
* @author Phuong LE <phuong.le@agence-soon.fr> <@>
* @category Namespace
* @package Namespace_Core
* @copyright Copyright (c) 2016 Agence Soon (http://www.agence-soon.fr)
*/
namespace Namespace\Core\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 WebsiteFactory
*/
private $websiteFactory;
/**
* @var Website
*/
private $websiteResourceModel;
/**
* @var StoreFactory
*/
private $storeFactory;
/**
* @var GroupFactory
*/
private $groupFactory;
/**
* @var Group
*/
private $groupResourceModel;
/**
* @var Store
*/
private $storeResourceModel;
/**
* @var ManagerInterface
*/
private $eventManager;
/**
* 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(
WebsiteFactory $websiteFactory,
Website $websiteResourceModel,
Store $storeResourceModel,
Group $groupResourceModel,
StoreFactory $storeFactory,
GroupFactory $groupFactory,
ManagerInterface $eventManager
) {
$this->websiteFactory = $websiteFactory;
$this->websiteResourceModel = $websiteResourceModel;
$this->storeFactory = $storeFactory;
$this->groupFactory = $groupFactory;
$this->groupResourceModel = $groupResourceModel;
$this->storeResourceModel = $storeResourceModel;
$this->eventManager = $eventManager;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var \Magento\Store\Model\Website $website */
$website = $this->websiteFactory->create();
$website->load('my_custom_code');
if(!$website->getId()){
$website->setCode('my_custom_code');
$website->setName('My Custom Name');
$website->setDefaultGroupId(3);
$this->websiteResourceModel->save($website);
}
if($website->getId()){
/** @var \Magento\Store\Model\Group $group */
$group = $this->groupFactory->create();
$group->setWebsiteId($website->getWebsiteId());
$group->setName('My Custom Group Name');
$group->setRootCategoryId(2);
$group->setDefaultStoreId(3);
$this->groupResourceModel->save($group);
}
/** @var \Magento\Store\Model\Store $store */
$store = $this->storeFactory->create();
$store->load('my_custom_store_code');
if(!$store->getId()){
$group = $this->groupFactory->create();
$group->load('My Custom Group Name', 'name');
$store->setCode('my_custom_store_code');
$store->setName('Mu Custom Store Code');
$store->setWebsite($website);
$store->setGroupId($group->getId());
$store->setData('is_active','1');
$this->storeResourceModel->save($store);
// Trigger event to insert some data to the sales_sequence_meta table (fix bug place order in checkout)
$this->eventManager->dispatch('store_add', ['store' => $store]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment