1.先创建一个模块
module.xml
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemeaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Test_Commands" setup_version="2.0.0"/>
</config>
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_Commands',
__DIR__
);
2.背景
3.创建目录和类
protected function configure()
{
$this->setName('catalog:category:clean')
->setDescription('Cleans the catalog from extra categories (ending with more than 4 numbers');
parent::configure();
}
4.Register Class with the CLI tool
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="infitechCommand" xsi:type="object">Test\Commands\Console\Command\CatalogCleanCommand</item>
</argument>
</arguments>
</type>
</config>
这时候运行命令会出错:
必须重载 execute() 方法,执行的动作写在里面。
<?php
namespace Test\Commands\Console\Command;
use Magento\Framework\App\ObjectManager\ConfigLoader;
use Magento\Framework\App\ObjectManagerFactory;
use Magento\Framework\App\State;
use Magento\Store\Model\StoreManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CatalogCleanCommand extends Command
{
protected $objectManager;
public function __construct(
ObjectManagerFactory $objectManagerFactory
){
$params = $_SERVER;
$params[StoreManager::PARAM_RUN_CODE] = 'admin';
$params[StoreManager::PARAM_RUN_TYPE] = 'store';
$this->objectManager = $objectManagerFactory->create($params);
parent::__construct();
}
protected function configure()
{
$this->setName('catalog:category:clean')
->setDescription('Cleans the catalog from extra categories (ending with more than 4 numbers');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<info>Starting Category Cleanup</info>');
/**
* @var \Magento\Framework\Registry
*/
$registry = $this->objectManager->get('\Magento\Framework\Registry');
$registry->register('isSecureArea', true);
/** @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory */
$categoryCollectionFactory = $this->objectManager->get('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection */
$categoryCollection = $categoryCollectionFactory->create();
$categoryCollection->addAttributeToSelect('name');
/** @var \Magento\Catalog\Model\Category $category */
foreach ($categoryCollection as $category) {
if (preg_match('/\\d{4,}$/', $category->getName()) == 1) {
$output->writeln('<comment>Deleting Category with Name: "'. $category->getName() . '"</comment>');
$category->delete();
var_dump($category);
}
}
$output->writeln('<info>Categories Cleaned</info>');
return 0;
}
}
http://www.clounce.com/magento/creating-shell-script-for-magento2