Magento 2 : How to create a store, a store group and a website programmatically

Magento 2 : How to create a store, a store group and a website programmatically

Whenever it comes during your projects, you can need to create a new store, a new store group and a new website. You can do it programmatically.

A store refers a store group to which “it is part” and a store group refers a website to “which it is part”. But a website also refers a default store group and and store group refers a default store.

You then need to decide what entity to create first, refer to other entities and then change the references after having created the final entities. The solution I have decided to use is to create the website, then the store group and finally the store.

The following code have to be place in an installer file (InstallData.php or UpgradeData.php placed in the Setup folder of a module. The class have to implements either \Magento\Framework\Setup\UpgradeDataInterface or \Magento\Framework\Setup\InstallDataInterface.

<?php

namespace Addeos\Core\Setup;

use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Store\Model\ResourceModel\Group as GroupResourceModel;
use Magento\Store\Model\ResourceModel\Store as StoreResourceModel;
use Magento\Store\Model\ResourceModel\Website as WebsiteResourceModel;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\WebsiteFactory;

class UpgradeData implements UpgradeDataInterface
{
    
    /**
     * UpgradeData constructor.
     * @param Installer $installer
     * @param ManagerInterface $eventManager
     * @param WebsiteResourceModel $websiteModel
     * @param GroupResourceModel $groupModel
     * @param StoreResourceModel $storeModel
     * @param WebsiteFactory $websiteFactory
     * @param GroupFactory $groupFactory
     * @param StoreFactory $storeFactory
     */
    public function __construct(
        Installer $installer,
        ManagerInterface $eventManager,
        WebsiteResourceModel $websiteModel,
        GroupResourceModel $groupModel,
        StoreResourceModel $storeModel,
        WebsiteFactory $websiteFactory,
        GroupFactory $groupFactory,
        StoreFactory $storeFactory,
    ) {
        $this->installer = $installer;
        $this->eventManager = $eventManager;
        $this->websiteResourceModel = $websiteModel;
        $this->groupResourceModel = $groupModel;
        $this->storeResourceModel = $storeModel;
        $this->websiteFactory = $websiteFactory;
        $this->groupFactory = $groupFactory;
        $this->storeFactory = $storeFactory;
    }
    
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.4', '<')) {
            $this->createEntities();
        }    
    }
    
    private function createEntities()
    {
        /** @var Magento\Store\Model\Website $website */
        $website = $this->websiteFactory->create();
        $website->setCode('website_code');
        $website->setName('website name');
        // Set an existing store group id
        $website->setDefaultGroupId(2);
        $this->websiteResourceModel->save($website);

        /** @var \Magento\Store\Model\Group $group */
        $group = $this->groupFactory->create();
        $group->setWebsiteId($website->getWebsiteId());
        $group->setName('store group name');
        // Set the category id for the root category of the store group
        $group->setRootCategoryId(2);
        // Set an existing store id
        $group->setDefaultStoreId(3); 
        $this->groupResourceModel->save($group);

        /** @var \Magento\Store\Model\Store $store */
        $store = $this->storeFactory->create();
        $store->setCode('store_code');
        $store->setName('store name');
        $store->setWebsite($website);
        $store->setGroupId($group->getId());
        $store->setData('is_active', '1');
        $this->storeResourceModel->save($store);
        $this->eventManager->dispatch('store_add', ['store' => $store]);

        // change the default group id for the website
        $website->setDefaultGroupId($group->getId());
        $this->websiteResourceModel->save($website);

        // change the default store id for the store group
        $group->setDefaultStoreId($store->getId());
        $this->groupResourceModel->save($group);
    }

}
berliozd