-
-
Save 0-Sony/1297fd13e11e9b901fa403a618b6cee5 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* This file is part of Namespace for Magento. | |
* | |
* @license All rights reserved | |
* @author Phuong LE <[email protected]> <@> | |
* @category Namespace | |
* @package Namespace_Core | |
* @copyright Copyright (c) 2016 Agence Soon (http://www.agence-soon.fr) | |
*/ | |
namespace Namespace\Core\Setup; | |
use Magento\Framework\Event\ManagerInterface; | |
use Magento\Framework\Setup\InstallDataInterface; | |
use Magento\Framework\Setup\ModuleContextInterface; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
use Magento\Store\Model\GroupFactory; | |
use Magento\Store\Model\ResourceModel\Group; | |
use Magento\Store\Model\ResourceModel\Store; | |
use Magento\Store\Model\ResourceModel\Website; | |
use Magento\Store\Model\StoreFactory; | |
use Magento\Store\Model\WebsiteFactory; | |
class InstallData implements InstallDataInterface | |
{ | |
/** | |
* @var WebsiteFactory | |
*/ | |
private $websiteFactory; | |
/** | |
* @var Website | |
*/ | |
private $websiteResourceModel; | |
/** | |
* @var StoreFactory | |
*/ | |
private $storeFactory; | |
/** | |
* @var GroupFactory | |
*/ | |
private $groupFactory; | |
/** | |
* @var Group | |
*/ | |
private $groupResourceModel; | |
/** | |
* @var Store | |
*/ | |
private $storeResourceModel; | |
/** | |
* @var ManagerInterface | |
*/ | |
private $eventManager; | |
/** | |
* InstallData constructor. | |
* @param WebsiteFactory $websiteFactory | |
* @param Website $websiteResourceModel | |
* @param Store $storeResourceModel | |
* @param Group $groupResourceModel | |
* @param StoreFactory $storeFactory | |
* @param GroupFactory $groupFactory | |
* @param ManagerInterface $eventManager | |
*/ | |
public function __construct( | |
WebsiteFactory $websiteFactory, | |
Website $websiteResourceModel, | |
Store $storeResourceModel, | |
Group $groupResourceModel, | |
StoreFactory $storeFactory, | |
GroupFactory $groupFactory, | |
ManagerInterface $eventManager | |
) { | |
$this->websiteFactory = $websiteFactory; | |
$this->websiteResourceModel = $websiteResourceModel; | |
$this->storeFactory = $storeFactory; | |
$this->groupFactory = $groupFactory; | |
$this->groupResourceModel = $groupResourceModel; | |
$this->storeResourceModel = $storeResourceModel; | |
$this->eventManager = $eventManager; | |
} | |
/** | |
* Installs data for a module | |
* | |
* @param ModuleDataSetupInterface $setup | |
* @param ModuleContextInterface $context | |
* @return void | |
*/ | |
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) | |
{ | |
/** @var \Magento\Store\Model\Website $website */ | |
$website = $this->websiteFactory->create(); | |
$website->load('my_custom_code'); | |
if(!$website->getId()){ | |
$website->setCode('my_custom_code'); | |
$website->setName('My Custom Name'); | |
$website->setDefaultGroupId(3); | |
$this->websiteResourceModel->save($website); | |
} | |
if($website->getId()){ | |
/** @var \Magento\Store\Model\Group $group */ | |
$group = $this->groupFactory->create(); | |
$group->setWebsiteId($website->getWebsiteId()); | |
$group->setName('My Custom Group Name'); | |
$group->setRootCategoryId(2); | |
$group->setDefaultStoreId(3); | |
$this->groupResourceModel->save($group); | |
} | |
/** @var \Magento\Store\Model\Store $store */ | |
$store = $this->storeFactory->create(); | |
$store->load('my_custom_store_code'); | |
if(!$store->getId()){ | |
$group = $this->groupFactory->create(); | |
$group->load('My Custom Group Name', 'name'); | |
$store->setCode('my_custom_store_code'); | |
$store->setName('Mu Custom Store Code'); | |
$store->setWebsite($website); | |
$store->setGroupId($group->getId()); | |
$store->setData('is_active','1'); | |
$this->storeResourceModel->save($store); | |
// Trigger event to insert some data to the sales_sequence_meta table (fix bug place order in checkout) | |
$this->eventManager->dispatch('store_add', ['store' => $store]); | |
} | |
} | |
} |
$this->eventManager->dispatch('store_add', ['store' => $store]);
seems to throw and exception [LogicException] There is no such indexer handler:
in CE 2.1.7
Hi, i'm trying to implement this. Where do you put it ? Ideally i need to make in in custom module, so i already have a custom page with block and controller. Can i use this in block ?
I'm starting my new steps in M2 world and I'm starting with your code...
Well done phuong ;)
@rev42 , you right ! thanks, i'm going to modify this file
@jclecas : You are welcome ! Have fun on M2 ;)
@Nundra Hmm yeah you can use your custom module, but i think it could be better to make another custom module called "core" maybe?
Put this code in your module only if it makes sense :)
https://gist.github.com/SpencerRohan/839933a564bbecfc6949af71dd1b1dfe Modified it a bit for multiple entry, does not include Root Category ID. Great snippet!
This line :
$this->eventManager->dispatch('store_add', ['store' => $store]);
Is it necessary with version magneto 2.4 ?
It seems to have been fixed. When running command-line bin/magento setup:upgrade, I always received an error: " Original exception message: DDL statements are not allowed in transactions"
@ngtuan96lc thanks, I tried and it's working even without event manager.
@ngtuan96lc thanks, I tried and it's working even without event manager.
This piece of code has been created when I worked on Magento 2.0 back in the days :) . Fortunately, you do not need it anymore on the latest versions
@ngtuan96lc thanks, I tried and it's working even without event manager.
This piece of code has been created when I worked on Magento 2.0 back in the days :) . Fortunately, you do not need it anymore on the latest versions
Correctly!
You should initialize $this->eventManager in the constructor