Skip to content

Create store view using the command line #29408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
322cb25
Added cli command to create store view
0m3r Aug 5, 2020
10352e9
Sterilized name and code arguments
0m3r Aug 5, 2020
1e21918
Merge branch '2.4-develop' into feature-create-store-view-command
VladimirZaets Aug 11, 2020
7949e0c
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Sep 14, 2020
28161a4
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Oct 6, 2020
050d238
Add StoreCreateManagment model
0m3r Oct 22, 2020
86c4b55
Applied some of @sivaschenko's suggestions
0m3r Oct 29, 2020
5d14696
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Nov 4, 2020
ed4a1fe
Prevent integration test failing (Table store_website doesn\'t exist)
0m3r Dec 1, 2020
fab0237
Merge branch 'feature-create-store-view-command' of github.com:0m3r/m…
0m3r Dec 1, 2020
d2e1f40
Add integration test for CreateStore
0m3r Dec 1, 2020
5e8ec78
Add GetDefaultStoreGroupTest
0m3r Dec 2, 2020
024fe1d
Applied some of @sivaschenko's suggestions
0m3r Jan 6, 2021
0d528ea
CreateStore::execute accept a StoreInterface instead of array
0m3r Jan 8, 2021
5e87d6e
Fixed PHPdoc param name typo
0m3r Jan 8, 2021
1a8e31c
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Jun 23, 2021
4c5188f
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Aug 5, 2021
b333b5b
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Sep 21, 2021
37f731d
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Sep 23, 2021
ff6ca5f
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Sep 24, 2021
25e08f3
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Sep 29, 2021
9b39023
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Oct 12, 2021
341a347
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Feb 16, 2022
f70a5d0
Merge branch '2.4-develop' into feature-create-store-view-command
0m3r Jun 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions app/code/Magento/Store/Console/Command/StoreCreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Store\Console\Command;

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* Class StoreCreateCommand
* Command for create a new the configured store
*/
class StoreCreateCommand extends Command
{
const INPUT_ARGUMENT_NAME = 'name';
const INPUT_ARGUMENT_CODE = 'code';
const INPUT_ARGUMENT_IS_ACTIVE = 'is_active';
const INPUT_ARGUMENT_SORT_ORDER = 'sort_order';

const INPUT_OPTION_GROUP = 'group_id';

/**
*
* @var \Magento\Store\Model\CreateStore
*/
private $createStore;

/**
*
* @var \Magento\Store\Model\GetDefaultStoreGroup
*/
private $getDefaultStoreGroup;

/**
* @var \Magento\Framework\Filter\FilterManager
*/
private $filterManager;

/**
*
* @param \Magento\Store\Model\CreateStore $createStore
* @param \Magento\Store\Model\GetDefaultStoreGroup $getDefaultStoreGroup
* @param \Magento\Framework\Filter\FilterManager $filterManager
* @param string $name
*/
public function __construct(
\Magento\Store\Model\CreateStore $createStore,
\Magento\Store\Model\GetDefaultStoreGroup $getDefaultStoreGroup,
\Magento\Framework\Filter\FilterManager $filterManager,
$name = null
) {
$this->createStore = $createStore;
$this->getDefaultStoreGroup = $getDefaultStoreGroup;
$this->filterManager = $filterManager;
parent::__construct($name);
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('store:create')
->setDescription('Create new store view (see list: bin/magento store:list)')
->addArgument(self::INPUT_ARGUMENT_NAME, InputArgument::REQUIRED, 'Put the store view name you want to create')
->addArgument(self::INPUT_ARGUMENT_CODE, InputArgument::REQUIRED, 'Put the store view code')
->addArgument(
self::INPUT_ARGUMENT_IS_ACTIVE,
InputArgument::OPTIONAL,
'Status (enable/disable)',
true
)
->addArgument(
self::INPUT_ARGUMENT_SORT_ORDER,
InputArgument::OPTIONAL,
'Sort Order',
0
)
;

$this->addOption(
self::INPUT_OPTION_GROUP,
'g',
InputOption::VALUE_OPTIONAL,
'Group ID (php bin/magento store:list).'
);

parent::configure();
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);

try {
$defaultGroupId = $input->getOption(self::INPUT_OPTION_GROUP);
if ($defaultGroupId === null) {
/** @var \Magento\Store\Api\Data\GroupInterface $defaultGroup */
$defaultGroup = $this->getDefaultStoreGroup->execute();
$defaultGroupId = $defaultGroup->getId();
}
$defaultGroupId = (int) $defaultGroupId;
$data = [
self::INPUT_OPTION_GROUP => (int) $input->getOption(
self::INPUT_OPTION_GROUP
),
self::INPUT_ARGUMENT_NAME => (string) $input->getArgument(
self::INPUT_ARGUMENT_NAME
),
self::INPUT_ARGUMENT_CODE => (string) $input->getArgument(
self::INPUT_ARGUMENT_CODE
),
self::INPUT_ARGUMENT_IS_ACTIVE => (bool) $input->getArgument(
self::INPUT_ARGUMENT_IS_ACTIVE
),
self::INPUT_ARGUMENT_SORT_ORDER => (int) $input->getArgument(
self::INPUT_ARGUMENT_SORT_ORDER
)
];
$data[self::INPUT_ARGUMENT_NAME] = $this->filterManager->removeTags(
$data[self::INPUT_ARGUMENT_NAME]
);
$data[self::INPUT_ARGUMENT_CODE] = $this->filterManager->removeTags(
$data[self::INPUT_ARGUMENT_CODE]
);

$this->createStore->create($data);

$io->success('You created the store view.');

return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$io->error($e->getMessage());
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$io->comment($e->getTraceAsString());
}

return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
}
}
60 changes: 60 additions & 0 deletions app/code/Magento/Store/Model/CreateStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Store\Model;

use Magento\Store\Api\Data\StoreInterface;

/**
* Class CreateStore
* Model for create a new store view
*/
class CreateStore
{
/**
* @var \Magento\Store\Api\Data\GroupInterfaceFactory
*/
private $groupFactory;

/**
* @var \Magento\Store\Api\Data\StoreInterfaceFactory
*/
private $storeFactory;

/**
*
* @param \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory
* @param \Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory
*/
public function __construct(
\Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory,
\Magento\Store\Api\Data\StoreInterfaceFactory $storeFactory
) {
$this->groupFactory = $groupFactory;
$this->storeFactory = $storeFactory;
}

/**
*
* @param array $data
* @return \Magento\Store\Api\Data\StoreInterface
*/
public function execute(array $data): StoreInterface
{
/** @var \Magento\Store\Model\Storev $storeModel */
$storeModel = $this->storeFactory->create(['data' => $data]);

/** @var \Magento\Store\Model\Group $groupModel */
$groupModel = $this->groupFactory->create()
->load($storeModel->getGroupId());

$storeModel->setWebsiteId($groupModel->getWebsiteId());
$storeModel->save();

return $storeModel;
}
}
74 changes: 74 additions & 0 deletions app/code/Magento/Store/Model/GetDefaultStoreGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Store\Model;

use Magento\Store\Api\Data\GroupInterface;

/**
* Class GetDefaultStoreGroup
* Get default (first) store group;
*/
class GetDefaultStoreGroup
{
/**
* @var \Magento\Store\Model\WebsiteFactory
*/
private $websiteFactory;

/**
* @var \Magento\Store\Api\Data\GroupInterfaceFactory
*/
private $groupFactory;

/**
*
* @param \Magento\Store\Api\Data\WebsiteInterfaceFactory $websiteFactory
* @param \Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory
*/
public function __construct(
\Magento\Store\Api\Data\WebsiteInterfaceFactory $websiteFactory,
\Magento\Store\Api\Data\GroupInterfaceFactory $groupFactory
) {
$this->websiteFactory = $websiteFactory;
$this->groupFactory = $groupFactory;
}

/**
* Get default group
*
* @return GroupInterface
*/
public function execute() : GroupInterface
{
$groups = $this->getAllStoreGroups();

return current($groups);
}


/**
* Retrieve list of store groups
*
* @return array
*/
private function getAllStoreGroups() : array
{
$websites = $this->websiteFactory->create()->getCollection();
$allgroups = $this->groupFactory->create()->getCollection();
$groups = [];
foreach ($websites as $website) {
foreach ($allgroups as $group) {
if ($group->getWebsiteId() == $website->getId()) {
$groups[$group->getWebsiteId() . '-' . $group->getId()] = $group;
}
}
}

return $groups;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Store/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@
<arguments>
<argument name="commands" xsi:type="array">
<item name="commandStoreList" xsi:type="object">Magento\Store\Console\Command\StoreListCommand</item>
<item name="commandStoreCreate" xsi:type="object">Magento\Store\Console\Command\StoreCreateCommand</item>
<item name="commandWebsiteList" xsi:type="object">Magento\Store\Console\Command\WebsiteListCommand</item>
</argument>
</arguments>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Store\Model;

class CreateStoreTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $objectManager;

/**
* @var \Magento\Store\Model\CreateStore
*/
protected $createStoreModel;

/**
* @var \Magento\Store\Model\Website
*/
protected $websiteModel;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

$this->createStoreModel = $this->objectManager->get(
\Magento\Store\Model\CreateStore::class
);

$this->websiteModel = $this->objectManager->create(
\Magento\Store\Model\Website::class
);
$this->websiteModel->load(1);
}

/**
* @param $data
* @dataProvider loadCreateDataProvider
*/
public function testExecute($data)
{
$defaultGroupId = $this->websiteModel->getDefaultGroupId();
$data['group_id'] = $defaultGroupId;

$store = $this->createStoreModel->execute($data);
$this->assertInstanceOf(\Magento\Store\Api\Data\StoreInterface::class, $store);
$this->assertNotNull($store->getId(), 'Store was not saved correctly');
$this->assertEquals($data['code'], $store->getCode());

/** @var \Magento\Framework\Registry $registry */
$registry = $this->objectManager->get(\Magento\Framework\Registry::class);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

$store->delete();

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
}

/**
* @return array
*/
public function loadCreateDataProvider()
{
return [[
[
'name' => 'code',
'code' => 'code',
'is_active' => 0,
'sort_order' => 10,
]
]];
}
}
Loading