Skip to content

Instantly share code, notes, and snippets.

@jclecas
Last active January 29, 2018 13:59
Show Gist options
  • Save jclecas/771f983d6e10c90211b11094e5bc7829 to your computer and use it in GitHub Desktop.
Save jclecas/771f983d6e10c90211b11094e5bc7829 to your computer and use it in GitHub Desktop.
Magento 2 / Setup websites, stores, views with ID predefined
<?php
/**
* This file is part of Namespace\Websites\Helper for Magento 2.
*
* @license JC Lecas
* @author JC Lecas <jclecas@yahoo.fr>
* @category Namespace\Websites\Helper
*/
namespace Namespace\Websites\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
class Config extends AbstractHelper
{
public $websites = [
['id' => 1, 'code' => 'namespace_fr', 'store' => 'store_fr', 'view' => 'fr', 'area' => 'eu'],
['id' => 2, 'code' => 'namespace_en', 'store' => 'store_en', 'view' => 'en', 'area' => 'eu'],
['id' => 3, 'code' => 'namespace_es', 'store' => 'store_es', 'view' => 'es', 'area' => 'eu'],
['id' => 4, 'code' => 'namespace_it', 'store' => 'store_it', 'view' => 'it', 'area' => 'eu'],
['id' => 5, 'code' => 'namespace_pt', 'store' => 'store_pt', 'view' => 'pt', 'area' => 'eu'],
['id' => 6, 'code' => 'namespace_uk', 'store' => 'store_uk', 'view' => 'uk', 'area' => 'eu'],
['id' => 7, 'code' => 'namespace_de', 'store' => 'store_de', 'view' => 'de', 'area' => 'eu'],
['id' => 8, 'code' => 'namespace_us', 'store' => 'store_us', 'view' => 'us', 'area' => 'us'],
['id' => 9, 'code' => 'namespace_ru', 'store' => 'store_ru', 'view' => 'ru', 'area' => 'eu'],
['id' => 10, 'code' => 'namespace_br', 'store' => 'store_br', 'view' => 'br', 'area' => 'us'],
['id' => 11, 'code' => 'namespace_ca_en', 'store' => 'store_ca_en', 'view' => 'ca_en', 'area' => 'us'],
['id' => 12, 'code' => 'namespace_ca_fr', 'store' => 'store_ca_fr', 'view' => 'ca_fr', 'area' => 'us'],
['id' => 13, 'code' => 'namespace_be_fr', 'store' => 'store_be_fr', 'view' => 'be_fr', 'area' => 'eu'],
['id' => 14, 'code' => 'namespace_tr', 'store' => 'store_tr', 'view' => 'tr', 'area' => 'eu'],
['id' => 15, 'code' => 'namespace_cn', 'store' => 'store_cn', 'view' => 'cn', 'area' => 'cn'],
['id' => 16, 'code' => 'namespace_hk_en', 'store' => 'store_hk_en', 'view' => 'hk_en', 'area' => 'as'],
['id' => 17, 'code' => 'namespace_hk_cn', 'store' => 'store_hk_cn', 'view' => 'hk_cn', 'area' => 'as'],
['id' => 18, 'code' => 'namespace_be_nl', 'store' => 'store_be_nl', 'view' => 'be_nl', 'area' => 'eu'],
['id' => 19, 'code' => 'namespace_kr', 'store' => 'store_kr', 'view' => 'kr', 'area' => 'as'],
['id' => 20, 'code' => 'namespace_mx', 'store' => 'store_mx', 'view' => 'mx', 'area' => 'us'],
['id' => 21, 'code' => 'namespace_nl', 'store' => 'store_nl', 'view' => 'nl', 'area' => 'eu'],
['id' => 22, 'code' => 'namespace_pl', 'store' => 'store_pl', 'view' => 'pl', 'area' => 'eu'],
['id' => 23, 'code' => 'namespace_ae_en', 'store' => 'store_ae_en', 'view' => 'ae_en', 'area' => 'eu'],
['id' => 24, 'code' => 'namespace_ae_ar', 'store' => 'store_ae_ar', 'view' => 'ae_ar', 'area' => 'eu']
];
public $countryLabels = [
'fr' => 'France',
'en' => 'International EN',
'es' => 'Spain',
'it' => 'Italy',
'pt' => 'Portugal',
'uk' => 'United Kingdom',
'de' => 'Germany',
'us' => 'United States',
'ru' => 'Russia',
'br' => 'Brazil',
'ca_en' => 'Canada EN',
'ca_fr' => 'Canada FR',
'be_fr' => 'Belgium FR',
'tr' => 'Turkey',
'cn' => 'China',
'hk_en' => 'Hong Kong EN',
'hk_cn' => 'Hong Kong CN',
'be_nl' => 'Belgium NL',
'kr' => 'Korea',
'mx' => 'Mexico',
'nl' => 'Netherlands',
'pl' => 'Poland',
'ae_en' => 'United Arab Emirates EN',
'ae_ar' => 'United Arab Emirates AR'
];
public $websiteNames = [
'namespace_fr' => 'Namespace FR',
'namespace_en' => 'Namespace EN',
'namespace_es' => 'Namespace ES',
'namespace_it' => 'Namespace IT',
'namespace_pt' => 'Namespace PT',
'namespace_uk' => 'Namespace UK',
'namespace_de' => 'Namespace DE',
'namespace_us' => 'Namespace US',
'namespace_ru' => 'Namespace RU',
'namespace_br' => 'Namespace BR',
'namespace_ca_en' => 'Namespace CA EN',
'namespace_ca_fr' => 'Namespace CA FR',
'namespace_be_fr' => 'Namespace BE FR',
'namespace_tr' => 'Namespace TR',
'namespace_cn' => 'Namespace CN',
'namespace_hk_en' => 'Namespace HK EN',
'namespace_hk_cn' => 'Namespace HK CN',
'namespace_be_nl' => 'Namespace BE NL',
'namespace_kr' => 'Namespace KR',
'namespace_mx' => 'Namespace MX',
'namespace_nl' => 'Namespace NL',
'namespace_pl' => 'Namespace PL',
'namespace_ae_en' => 'Namespace AE EN',
'namespace_ae_ar' => 'Namespace AE AR'
];
/**
* Get country label
*
* @param $code
* @return mixed
*/
public function getCountryLabel($code)
{
if (array_key_exists($code, $this->countryLabels)) {
return $this->countryLabels[$code];
}
return $code;
}
/**
* Get Website name
*
* @param $code
* @return mixed
*/
public function getWebsiteName($code)
{
if (array_key_exists($code, $this->websiteNames)) {
return $this->websiteNames[$code];
}
return $code;
}
/**
* Get websites
*
* @return array
*/
public function getWebsites()
{
return $this->websites;
}
}
<?php
/**
* This file is part of Namespace\Websites\Setup for Magento 2.
*
* @license JC Lecas
* @author JC Lecas <jclecas@yahoo.fr>
* @category Namespace\Core\Setup
*/
namespace Namespace\Websites\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Namespace\Websites\Helper\Config;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Model\StoreFactory;
class InstallData implements InstallDataInterface
{
/**
* @var Config Config
*/
private $config;
/**
* @var ManagerInterface
*/
private $eventManager;
/**
* @var StoreFactory
*/
private $storeFactory;
/**
* @var ModuleDataSetupInterface
*/
private $setup;
/**
* InstallData constructor.
*
* @param Config $config
* @param StoreFactory $storeFactory
* @param ManagerInterface $eventManager
*/
public function __construct(
Config $config,
StoreFactory $storeFactory,
ManagerInterface $eventManager
) {
$this->config = $config;
$this->storeFactory = $storeFactory;
$this->eventManager = $eventManager;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
* @throws \Exception
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
// Clean tables
$this->setup = $setup;
$this->cleanTables();
$insertWebsites = [];
$insertGroups = [];
$insertStores = [];
// Prepare data
$isDefault = true;
$websites = $this->config->getWebsites();
foreach ($websites as $data) {
// Websites
$insertWebsites[] = [
'website_id' => $data['id'],
'code' => $data['code'],
'name' => $this->config->getWebsiteName($data['code']),
'sort_order' => $data['id'],
'is_default' => ($isDefault ? 1 : 0),
'default_group_id' => $data['id']
];
$isDefault = false;
// Stores
$insertGroups[] = [
'group_id' => $data['id'],
'website_id' => $data['id'],
'code' => $data['store'],
'root_category_id' => 2,
'default_store_id' => $data['id'],
'name' => $this->config->getCountryLabel($data['view'])
];
// Views
$insertStores[] = [
'store_id' => $data['id'],
'website_id' => $data['id'],
'group_id' => $data['id'],
'code' => $data['view'],
'name' => $this->config->getCountryLabel($data['view']),
'sort_order' => $data['id'],
'is_active' => 1
];
}
// Insert data
$connection = $setup->getConnection();
$connection->insertMultiple($connection->getTableName('store_website'), $insertWebsites);
$connection->insertMultiple($connection->getTableName('store_group'), $insertGroups);
$connection->insertMultiple($connection->getTableName('store'), $insertStores);
// Event manager dispatch store
$collection = $this->storeFactory->create()->getCollection();
foreach ($collection as $store) {
$this->eventManager->dispatch('store_add', ['store' => $store]);
}
$setup->endSetup();
}
/**
* Clean and drop tables
*/
public function cleanTables()
{
$this->setup->run('DELETE FROM `store` WHERE store_id > 0;');
$this->setup->run('DELETE FROM `store_group` WHERE group_id > 0;');
$this->setup->run('DELETE FROM `store_website` WHERE website_id > 0;');
$connexion = $this->setup->getConnection();
for ($i = 1; $i <= 24; $i++) {
$connexion->dropTable("catalogsearch_fulltext_scope{$i}");
$connexion->dropTable("sequence_creditmemo_{$i}");
$connexion->dropTable("sequence_invoice_{$i}");
$connexion->dropTable("sequence_order_{$i}");
$connexion->dropTable("sequence_rma_item_{$i}");
$connexion->dropTable("sequence_shipment_{$i}");
}
$connexion->truncateTable('sales_sequence_profile');
$connexion->truncateTable('sales_sequence_meta');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment