I want to create stores/store_views programmatically from the frontend. Is it possible to do that?

I have not found any useful tutorial explaining to create stores/store views.

I am using Magento 2.4.1

2 Answers 2


You can use below code to create store programetically.

/** @var  \Magento\Store\Model\Store $store */
$store = $this->storeFactory->create();
    $group = $this->groupFactory->create();
    $group->load('My Custom Group Name', 'name');
    $store->setName('Mu Custom Store Code');
    // 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]);

where :

storeFactory = Magento\Store\Model\StoreFactory
groupFactory = Magento\Store\Model\GroupFactory
$website = Magento\Store\Model\Website
storeResourceModel = Magento\Store\Model\ResourceModel\Website
  • Thank you for the code, I have added my own answer to the question. For anyone who is looking to achieve the same result. Jan 18, 2021 at 8:38
  • There is a anot way to create it addeos.com/…
    – Abbas
    Nov 18, 2021 at 1:56

Here I am posting my answer. I hope this will help someone

    namespace Vendor\ModuleName\Controller;
    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;
    use Magento\Framework\Event\ManagerInterface;
    class myClass extends \Magento\Framework\App\Action\Action{
        protected $groupResourceModel;
        protected $storeResourceModel;
        protected $websiteFactory;
        protected $websiteResourceModel;
        protected $groupFactory;
        protected $eventManager;
        protected $storeFactory;
         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;
        public function execute(){
            $attribute =[
                    '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'
            $store = $this->storeFactory->create();
                /** @var \Magento\Store\Model\Website $website */
                $website = $this->websiteFactory->create();
                $website = $this->setWebID($website, $attribute);
                /** @var \Magento\Store\Model\Group $group */
                $group = $this->groupFactory->create();
                $group = $this->groupFactory->create();
                $group->load($attribute['group_name'], 'name');
                $store->setData('is_active', $attribute['is_active']);
                $this->eventManager->dispatch('store_add', ['store' => $store]);
                $store = $this->storeFactory->create();
return true;
            public function setWebID($website, $attribute){
                return $website;

This will create your website, store, store view

  • There is a anot way to create it addeos.com/…
    – Abbas
    Nov 18, 2021 at 1:57
  • Thank you for posting your solution. Helped me a lot.
    – Dolly
    Mar 20 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.