Adding new CLI Command Magento 2?

In this post, we are going to learn about Command-line Interface(CLI) in Magento 2. There have a lot of commands in bin/magento. I’m going to explain about adding our custom CLI in Magento 2.

Okay let’s run following command in terminal:

php bin/magento

php bin/magento

You can see a list of CLI commands.We, Will, check to add our custom command to the list.

1- Create a module

Im using my module as


Create di.xml file


<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="magechallenge_order_update" xsi:type="object">MageChallenge\SetupData\Command\SendOrderData</item>

Create new SendOrderData.php file

File Path: app/code/MageChallenge/SetupData/Command/SendOrderData.php

namespace MageChallenge\SetupData\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

 * Class SendOrderData
 * @package MageChallenge\SetupData\Command
class SendOrderData extends \Symfony\Component\Console\Command\Command
    const NAME = "magechallenge:order:update";
     * @var \Magento\Framework\App\State
    private $state;

     * SendOrderData constructor.
     * @param \Magento\Framework\App\State $state
    public function __construct(
        \Magento\Framework\App\State $state
    ) {
        $this->state = $state;

    protected function configure()
        $this->setDescription("Send Order Data to CvCRM");

     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|void|null
     * @throws \Magento\Framework\Exception\LocalizedException
    protected function execute(InputInterface $input, OutputInterface $output)
        $output->writeln("START ORDER DATA SENDING");
        // you can start writing you code from here
        $output->writeln("END ORDER DATA SENDING");

Okay. Now run following command

– php bin/magento cache:flush (This will clear cache )

– php bin/magento setup:di:compile (Generate Dependency Injection)

Now all finish. You can check your custom command in the list. to check run “php bin/magento”

Yes. Now you can run your custom command any time using the following commands.

php bin/magento magechallenge:order:update

Have a nice day. 🙂

Latest posts by Chathura Janaka Liyanage (see all)

3 thoughts on “Adding new CLI Command Magento 2?

  1. I needed to post you that little bit of observation to say thanks once again for your personal marvelous principles you’ve featured in this article. It is quite seriously generous of people like you to give openly just what many individuals might have marketed as an e book to generate some dough for themselves, most notably given that you could possibly have done it if you ever decided. These points as well served to be a fantastic way to fully grasp other people have similar fervor much like my own to know the truth lots more when considering this matter. I believe there are several more pleasant sessions in the future for many who read through your site.

  2. Thanks for your whole work on this website. My niece takes pleasure in working on research and it is simple to grasp why. We all notice all relating to the powerful ways you render worthwhile tricks via your website and therefore welcome response from website visitors on this topic while our own simple princess is without question understanding so much. Take pleasure in the rest of the year. Your performing a wonderful job.

Leave a Reply