Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Last active January 27, 2026 15:23
Show Gist options
  • Select an option

  • Save 0-Sony/1297fd13e11e9b901fa403a618b6cee5 to your computer and use it in GitHub Desktop.

Select an option

Save 0-Sony/1297fd13e11e9b901fa403a618b6cee5 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]);
}
}
}
@0-Sony
Copy link
Author

0-Sony commented Jan 18, 2018

@Nundra Hmm yeah you can use your custom module, but i think it could be better to make another custom module called "core" maybe?
Put this code in your module only if it makes sense :)

@SpencerRohan
Copy link

https://gist.github.com/SpencerRohan/839933a564bbecfc6949af71dd1b1dfe Modified it a bit for multiple entry, does not include Root Category ID. Great snippet!

@ngtuan96lc
Copy link

This line :
$this->eventManager->dispatch('store_add', ['store' => $store]);
Is it necessary with version magneto 2.4 ?
It seems to have been fixed. When running command-line bin/magento setup:upgrade, I always received an error: " Original exception message: DDL statements are not allowed in transactions"

@hi-soni
Copy link

hi-soni commented Sep 3, 2020

@ngtuan96lc thanks, I tried and it's working even without event manager.

@0-Sony
Copy link
Author

0-Sony commented Dec 8, 2020

@ngtuan96lc thanks, I tried and it's working even without event manager.

This piece of code has been created when I worked on Magento 2.0 back in the days :) . Fortunately, you do not need it anymore on the latest versions

@ngtuan96lc
Copy link

@ngtuan96lc thanks, I tried and it's working even without event manager.

This piece of code has been created when I worked on Magento 2.0 back in the days :) . Fortunately, you do not need it anymore on the latest versions
Correctly!

@vikanav006-stack
Copy link

Often store owners underestimate backend security, focusing only on frontend features while ignoring critical risks like shared credentials, unused admin accounts, and outdated extensions. Weak login protection and reused passwords remain surprisingly common attack vectors that invite brute-force and credential-stuffing attacks. To strengthen access control, a solid step is adopting enhanced login verification such as best official source for Magento tools which adds another layer of defense beyond passwords. Before you implement it, review your user roles, revoke access for inactive accounts, and enforce strong, unique passwords with regular rotation. I also recommend periodic security audits, server hardening, and monitoring logs for unusual activity. Assess your setup by testing recovery procedures and simulating unauthorized access attempts. A dependable security posture not only protects data but also builds long-term trust with customers and partners, reducing disruption from potential breaches.

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