-
-
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 <phuong.le@agence-soon.fr> <@> | |
| * @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]); | |
| } | |
| } | |
| } |
@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!
Often store owners underestimate backend security, focusing only on frontend features while ignoring critical risks like shared credentials, unused admin accounts, and outdated extensions. Weak login protection and reused passwords remain surprisingly common attack vectors that invite brute-force and credential-stuffing attacks. To strengthen access control, a solid step is adopting enhanced login verification such as best official source for Magento tools which adds another layer of defense beyond passwords. Before you implement it, review your user roles, revoke access for inactive accounts, and enforce strong, unique passwords with regular rotation. I also recommend periodic security audits, server hardening, and monitoring logs for unusual activity. Assess your setup by testing recovery procedures and simulating unauthorized access attempts. A dependable security posture not only protects data but also builds long-term trust with customers and partners, reducing disruption from potential breaches.
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"