1

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;
} 
0

in test.php file in magento root:

<?php 

ini_set('display_errors', 1); 
error_reporting(E_ALL | E_STRICT); 


use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$websiteFactory = $obj->get('\Magento\Store\Model\WebsiteFactory');
$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.

  • Thanks! what if I wanted to create a store view? – Haim Jun 1 '16 at 1:11
  • @Haim I haven't looked at store views but it should be the same process using the StoreManagerInterface... – Claudiu Creanga Jun 1 '16 at 10:14
  • I tried but I couldn't get it, must be missing something simple with Magento 2 – Haim Jun 1 '16 at 11:45
0

According to: php v.7.* and magento 2.1.3. IMO/tested:

<?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 Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager  = $bootstrap->getObjectManager();
$appState       = $objectManager->get('Magento\Framework\App\State');
// echo \Magento\Framework\App\Area::AREA_ADMIN;
// echo \Magento\Framework\App\Area::AREA_ADMINHTML;
$appState->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$sessionManager = $objectManager->create('Magento\Framework\Session\SessionManagerInterface');
$sessionManager->start();


$storeModel     = $objectManager->create('Magento\Store\Model\Store');
$eventManager   = $objectManager->create('Magento\Framework\Event\ManagerInterface');
$messageManager = $objectManager->create('Magento\Framework\Message\ManagerInterface');
$filterManager  = $objectManager->create('Magento\Framework\Filter\FilterManager');

$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 \Magento\Store\Model\Store $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(
    'Magento\Store\Model\Group'
)->load(
    $postData['store']['group_id']
    // $storeModel->getGroupId()
);

$storeModel->setWebsiteId($postData['store']['website_id']);

if (!$storeModel->isActive() && $storeModel->isDefault()) {
    throw new \Magento\Framework\Exception\LocalizedException(
        __('The default store cannot be disabled')
    );
}

$storeModel->save();
$objectManager->get('Magento\Store\Model\StoreManager')->reinitStores();
$eventManager->dispatch($eventName, ['store' => $storeModel]);
  • specify the path also where this code will be integrated – VishalParkash Jan 12 at 11:03
0

you can try below code

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);


use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$storeManager = $obj->get('\Magento\Store\Model\StoreManagerInterface');
$websiteFactory = $obj->get('\Magento\Store\Model\WebsiteFactory');

$groupResourceModel=$obj->get('Magento\Store\Model\ResourceModel\Group');
$storeFactory=$obj->get('Magento\Store\Model\StoreFactory');
$groupFactory=$obj->get('Magento\Store\Model\GroupFactory');
$eventManager=$obj->get('Magento\Framework\Event');
$storeModel     = $obj->create('Magento\Store\Model\Store');

$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('Magento\Store\Model\StoreManager')->reinitStores();
                  $store->$eventManager->dispatch($eventName, ['store' => $storeModel]);
        }     

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.