Magento Asked by Haim on November 8, 2021
In Magento 1 i used something like this to create 2000 store views on the fly.
How would I do the same thing in Magento 2?
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
$strnm =1;
while ($strnm < 2001){
$store = Mage::getModel('core/store');
$store->setCode('tsm'.$strnm)
->setWebsiteId(1)
->setGroupId(1)
->setName('TSM'.$strnm )
->setIsActive(1)
->save();
$strnm += 1;
}
you can try below code
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$storeManager = $obj->get('MagentoStoreModelStoreManagerInterface');
$websiteFactory = $obj->get('MagentoStoreModelWebsiteFactory');
$groupResourceModel=$obj->get('MagentoStoreModelResourceModelGroup');
$storeFactory=$obj->get('MagentoStoreModelStoreFactory');
$groupFactory=$obj->get('MagentoStoreModelGroupFactory');
$eventManager=$obj->get('MagentoFrameworkEvent');
$storeModel = $obj->create('MagentoStoreModelStore');
$new_store = $websiteFactory->create();
$new_store->setName("da");
$new_store->setCode("da");
$new_store->save();
if ($new_store->getId()) {
$group = $groupFactory->create();
$group->setWebsiteId($new_store->getWebsiteId());
$group->setName('My Custom Group Name');
$group->setRootCategoryId(2);
$group->setDefaultStoreId(3);
$group->save();
}
$store = $storeFactory->create();
$store->load('my_custom_store_code');
if(!$store->getId()){
$group = $groupFactory->create();
$group->load('My Custom Group Name', 'name');
$store->setCode('my_custom_store_code');
$store->setName('Mu Custom Store Code');
$store->setWebsite($new_store);
$store->setGroupId($group->getId());
$store->setData('is_active','1');
$store->save($store);
// Trigger event to insert some data to the sales_sequence_meta table (fix bug place order in checkout)
$obj->get('MagentoStoreModelStoreManager')->reinitStores();
$store->$eventManager->dispatch($eventName, ['store' => $storeModel]);
}
Answered by Rakesh Donga on November 8, 2021
<?php
// 1. cd /path/to/magento2root/
// 2. create file: create_stores.php
// check predifined data:
// $postData['store']['group_id'] = 2; // store tab
// $postData['store']['website_id'] = 2; // web site tab
// etc ...
// 3. execute: php create_stores.php
// 4. bin/magento cache:flush
// Rignt way: add current code to 'execute()' method of your command line extension
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$appState = $objectManager->get('MagentoFrameworkAppState');
// echo MagentoFrameworkAppArea::AREA_ADMIN;
// echo MagentoFrameworkAppArea::AREA_ADMINHTML;
$appState->setAreaCode(MagentoFrameworkAppArea::AREA_ADMINHTML);
$sessionManager = $objectManager->create('MagentoFrameworkSessionSessionManagerInterface');
$sessionManager->start();
$storeModel = $objectManager->create('MagentoStoreModelStore');
$eventManager = $objectManager->create('MagentoFrameworkEventManagerInterface');
$messageManager = $objectManager->create('MagentoFrameworkMessageManagerInterface');
$filterManager = $objectManager->create('MagentoFrameworkFilterFilterManager');
$postData['store']['name'] = 'ru_RU';
$postData['store']['code'] = 'ru_ru';
$postData['store']['store_id'] = '';
$postData['store']['group_id'] = 2;
$postData['store']['website_id'] = 2;
$postData['store']['is_active'] = 1;
$postData['store']['sort_order'] = '';
$postData['store']['is_default'] = '';
$postData['store']['store_type'] = 'store';
$postData['store']['store_action'] = 'add';
foreach ($postData['store'] as $key => $value) {
$postData['store'][$key] = $filterManager->removeTags($value);
}
$eventName = 'store_edit';
/** @var MagentoStoreModelStore $storeModel */
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 = $objectManager->create(
'MagentoStoreModelGroup'
)->load(
$postData['store']['group_id']
// $storeModel->getGroupId()
);
$storeModel->setWebsiteId($postData['store']['website_id']);
if (!$storeModel->isActive() && $storeModel->isDefault()) {
throw new MagentoFrameworkExceptionLocalizedException(
__('The default store cannot be disabled')
);
}
$storeModel->save();
$objectManager->get('MagentoStoreModelStoreManager')->reinitStores();
$eventManager->dispatch($eventName, ['store' => $storeModel]);
Answered by coresh on November 8, 2021
in test.php file in magento root:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$storeManager = $obj->get('MagentoStoreModelStoreManagerInterface');
$websiteFactory = $obj->get('MagentoStoreModelWebsiteFactory');
$new_store = $websiteFactory->create();
$new_store->setName("da");
$new_store->setCode("da");
$new_store->save();
this will create a new website "da".
If you want a new store you should use the StoreFactory model
and also set the website id, root category etc and then create your loop.
Answered by Claudiu Creanga on November 8, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP