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
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou 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
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