2

How to display registered customers in terminal using custom console command?Can anyone please help me out.

4

First, you need to create di.xml in your customer module(I am assuming that you already know how to create a module)

app/code/VENDOR/MODULE/etc/di.xml

<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="registered_customer_list" xsi:type="object">VENDOR\MODULE\Console\Command\GetRegisteredCustomerList</item>
                </argument>
            </arguments>
        </type>    
</config>

Now you need to create GetRegisteredCustomerList.php

app/code/VENDOR/MODULE/Console/Command/GetRegisteredCustomerList.php

<?php

namespace Vendor\MODULE\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;


class GetRegisteredCustomerList extends Command
{
    const COMMAND = 'customer:list';


    private $customerCollectionFactory;


    public function __construct(
        CollectionFactory $customerCollectionFactory
    )
    {
        parent::__construct();

        $this->customerCollectionFactory = $customerCollectionFactory;
    }

    /**
     * @inheritdoc
     */
    protected function configure()
    {
        $this->setName(self::COMMAND);
        $desc = 'Registered customers list';
        $this->setDescription($desc);
    }


    protected function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            $table = $this->getHelperSet()->get('table');
            $table->setHeaders(['ID', 'Website ID', 'Group ID', 'First Name', 'Last Name', 'Email']);

            $customerCollection = $this->customerCollectionFactory->create();
            foreach ($customerCollection as $customer) {
                $table->addRow([
                    $customer->getData('entity_id'),
                    $customer->getData('website_id'),
                    $customer->getData('group_id'),
                    $customer->getData('firstname'),
                    $customer->getData('lastname'),
                    $customer->getData('email'),
                ]);
            }

            $table->render($output);
        } catch (\Exception $e) {
            $output->writeln("<error>Error executing command: {$e->getMessage()}</error>");
        }
    }
}

Now you need to execute the php bin/magento setup:di:compile command from the magento root directory

and php bin/magento cache:clean

And now you can get your registered customer list by executing below command,

php bin/magento customer:list

it will print something like this,

$ php bin/magento customer:list
+----+------------+----------+------------+-----------+------------------------------+
| ID | Website ID | Group ID | First Name | Last Name | Email                        |
+----+------------+----------+------------+-----------+------------------------------+
| 1  | 1          | 1        | Keyur      | Shah1     | keyurtest@gmail.com   |
| 2  | 1          | 1        | Keyur      | Shah      | keyur@gmail.com              |
| 3  | 1          | 1        | Keyur      | Shah      | keyurdemo@gmail.com  |
| 4  | 1          | 1        | Keyur      | Shah      | demo@gmail.com |
| 5  | 1          | 1        | Keyur      | Shah      | test@gmail.com    |
+----+------------+----------+------------+-----------+------------------------------+
3

You can add your own commands for listing customers:

add your command class through di.

<type name="Magento\Framework\Console\CommandListInterface">
    <arguments>
        <argument name="commands" xsi:type="array">
            <item name="customersList" xsi:type="object">Comapany\ModuleName\Console\Command\CustomersList</item>
        </argument>
    </arguments>
</type>

Create your class. Take help from magento created command classes. Here i create class from Magento\Catalog\Console\Command\ProductAttributesCleanUp

<?php

namespace Comapany\ModuleName\Console\Command;

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

/**
 * Class ProductAttributesCleanUp
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class CustomersList extends \Symfony\Component\Console\Command\Command
{

     /**
     * @var \Magento\Framework\App\State
     */
    protected $appState;

    /**
     * @var  \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
     */
    protected $customerCollectionFactory;


    /**
     * @param \Magento\Framework\App\State $appState
     * @param \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory
     */
    public function __construct(
        \Magento\Framework\App\State $appState,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerCollectionFactory

    ) {
        $this->appState = $appState;
        $this->customerCollectionFactory = $customerCollectionFactory;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('customers:list');
        $this->setDescription('List All Customers.');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->setDecorated(true);
        $this->appState->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);


        $output->writeln("");
        $collection =$this->customerCollectionFactory->create();
        $output->writeln("<info>Customers:</info>");
        foreach ($collection as $customer) {
            $output->writeln("<comment> ".$customer->getEmail()." </comment>");
        }


        return \Magento\Framework\Console\Cli::RETURN_SUCCESS;

    }


}
  • [Symfony\Component\Console\Exception\CommandNotFoundException] There are no commands defined in the "customers" namespace. Did you mean one of these? customer customer:hash – Vishali Mariappan Dec 17 '18 at 8:17
  • missed to update the name of command ,now updated my post name="customersList" . after making changes run setup:upgrade and cache:flush command – satya prakash patel Dec 17 '18 at 9:05
  • yes, check the updated post, it will display the email of all customers . you can update it according to your need. – satya prakash patel Dec 17 '18 at 9:16
  • Argument 2 passed to Codem\Timeslot\Console\Command\CustomersList::__construct() must be an instance of Magento\Customer\Model\ResourceModel\Customer\CollectionFactory, none given, called in /Library/WebServer/Documents/magento/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 111 and defined in /Library/WebServer/Documents/magento/app/code/Codem/Timeslot/Console/Command/CustomersList.php on line 30...This error appears – Vishali Mariappan Dec 17 '18 at 9:22
  • clean your generated directory and try cache flush – satya prakash patel Dec 17 '18 at 9:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.