0

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

1

You can use below code to create store programetically.

/** @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]);
}    

where :

storeFactory = Magento\Store\Model\StoreFactory
groupFactory = Magento\Store\Model\GroupFactory
$website = Magento\Store\Model\Website
storeResourceModel = Magento\Store\Model\ResourceModel\Website
2
  • 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
1

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

    <?php
    
    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();
            $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();
return true;
            }
    
            public function setWebID($website, $attribute){
                if(!$website->getId()){
                    $website->setCode($attribute['website_code']);
                    $website->setName($attribute['website_name']);
                    $this->websiteResourceModel->save($website);
                }
                return $website;
            }
        }
    }

This will create your website, store, store view

2
  • 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.