16. Shell scripts

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值