I want to create store or store view programmatically and I use the followng code (for store view):

    $storeView = $objectManager->create(\Magento\Store\Model\Store::class); // Store - is a store view; Group - is a store (this is confusing)
    $storeView->setGroupId(2); // GroupId is a Store ID (in adminhtml terms)

But I see in IDE that save() method is deprecated. There is a repo for Store model (store view in adminhtml terms) - \Magento\Store\Model\StoreRepository but this class has no save() or create() method (v Magento 2.1.0 RC2).

What is the right way to create store/store_views in Magento 2?


2 Answers 2


Unfortunately, as of Magento 2.1-RC2 the Service Contracts method for store creation have not been implemented yet.

I'm afraid you'll have to stick with the method you used for now as Magento core files uses the same method, see Magento/Backend/Controller/Adminhtml/System/Store/Save.php :

$storeModel = $this->_objectManager->create('Magento\Store\Model\Store');
$postData['store']['name'] = $this->filterManager->removeTags($postData['store']['name']);
if ($postData['store']['store_id']) {
if ($postData['store']['store_id'] == '') {
    $eventName = 'store_add';
$groupModel = $this->_objectManager->create(
if (!$storeModel->isActive() && $storeModel->isDefault()) {
    throw new \Magento\Framework\Exception\LocalizedException(
        __('The default store cannot be disabled')

The notice you get from the IDE regarding the save() method comes from the fact that the top level AbstractModel method is marked as deprecated thus, every class extending this class get the warning.


Thanks to @raphael-at-digital-pianism. I have the following code to create stores ("store views" as wrote in "adminhtml"):

 * Update/create store (frontname: "Store View").
 * @param int $storeId ID to update existing store or 'null' to create new one
 * @param string $name
 * @param string $code
 * @param int $websiteId
 * @param int $groupId (frontname: "Store")
 * @param int $sortOrder
 * @param bool $isActive
 * @return \Magento\Store\Model\Store
private function saveStore(
    $storeId = null,
    $isActive = false,
    $name = null,
    $code = null,
    $websiteId = null,
    $groupId = null,
    $sortOrder = null
) {
    $event = 'store_add';
    /** @var \Magento\Store\Model\Store $store */
    $store = $this->_manObj->create(\Magento\Store\Model\Store::class);
    /* 'code' is required attr. and should be set for existing store */
    $event = is_null($store->getCode()) ? 'store_add' : 'store_edit';
    if (!is_null($name)) $store->setName($name);
    if (!is_null($code)) $store->setCode($code);
    if (!is_null($websiteId)) $store->setWebsiteId($websiteId);
    if (!is_null($websiteId)) $store->setGroupId($groupId);
    if (!is_null($sortOrder)) $store->setSortOrder($sortOrder);
    /** @var  \Magento\Store\Model\StoreManager */
    /** @var  \Magento\Framework\Event\ManagerInterface */
    $this->manEvent->dispatch($event, ['store' => $store]);
    return $store;

This code works for Magento 2.1.3. I had an exception on 'store_edit' event when I changed 'default' code for default store view.

  • can you provide the complete code for this? Jan 15, 2021 at 10:21
  • Sorry, it was too many time ago. I suppose that unknown instance props are these types: $this->_manObj => \Magento\Framework\ObjectManagerInterface, $this->manStore => \Magento\Store\Model\StoreManagerInterface, $this->manEvent => \Magento\Framework\Event\ManagerInterface. Just inject these types into your constructor using Mage DI then add this method (saveStore(...))
    – Alex Gusev
    Jan 16, 2021 at 9:09
  • thanks , i will try this Jan 16, 2021 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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