Create website ,store and storeview through programmatically for Magento2

(1) Save this in file in root and run it . it will create automatically



<?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]);
        }      

Comments

Popular posts from this blog

Get Swatch Text Product id of Configurable Product in Detail Page through Jquery Magento2

Get Customer Address with customer ID programmatically Magento2

programmaticlly Create Order in Magento2 with custom options in order