4

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->setName('EN');
    $storeView->setCode('store2_en');
    $storeView->setWebsiteId(1);
    $storeView->setGroupId(2); // GroupId is a Store ID (in adminhtml terms)
    $storeView->setSortOrder(10);
    $storeView->setIsActive(true);
    $storeView->save();

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?

Thanks.

4

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']) {
    $storeModel->load($postData['store']['store_id']);
}
$storeModel->setData($postData['store']);
if ($postData['store']['store_id'] == '') {
    $storeModel->setId(null);
    $eventName = 'store_add';
}
$groupModel = $this->_objectManager->create(
    'Magento\Store\Model\Group'
)->load(
    $storeModel->getGroupId()
);
$storeModel->setWebsiteId($groupModel->getWebsiteId());
if (!$storeModel->isActive() && $storeModel->isDefault()) {
    throw new \Magento\Framework\Exception\LocalizedException(
        __('The default store cannot be disabled')
    );
}
$storeModel->save();

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.

0

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);
    $store->load($storeId);
    /* 'code' is required attr. and should be set for existing store */
    $event = is_null($store->getCode()) ? 'store_add' : 'store_edit';
    $store->setIsActive($isActive);
    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);
    $store->save();
    /** @var  \Magento\Store\Model\StoreManager */
    $this->manStore->reinitStores();
    /** @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.

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.