Hello Magento Friends,
In today’s article, we will learn about How to Add a Progress Bar using Custom CLI Command in Magento 2.
Sometimes a customer needs to wait for the process to be completed while working with Magento. A progress bar is used when you want to indicate to the customer about the process and its status.
You can create a progress bar using custom CLI commands in Magento 2. Let’s learn how
Steps to Add a Progress Bar using Custom CLI Command in Magento 2:
Step 1: Navigate to the below path
app\code\Vendor\Extension\etc\di.xml
Now, add the below code
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Framework\Console\CommandListInterface"> <arguments> <argument name="commands" xsi:type="array"> <item name="vendor_progress_bar" xsi:type="object">Vendor\Extension\Console\Command\ProgressBarCommand</item> </argument> </arguments> </type> <type name="Vendor\Extension\Console\Command\ProgressBarCommand"> <arguments> <argument name="name" xsi:type="string">magecomp:custom:progress-bar</argument> </arguments> </type> </config>
Step 2: Then go to the following path
app\code\Vendor\Extension\Console\Command\ProgressBar.php
Then move to the below path
<?php namespace Vendor\Extension\Console\Command; use Magento\Directory\Model\ResourceModel\Country\CollectionFactory; use Magento\Framework\Console\Cli; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Helper\ProgressBarFactory; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class ProgressBar extends Command { /** * @var ProgressBarFactory */ private $progressBarFactory; /** * @var CollectionFactory */ protected $_customer; protected $_customerFactory; /** * @inheritDoc */ public function __construct( ProgressBarFactory $progressBarFactory, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Model\Customer $customers, string $name ) { $this->progressBarFactory = $progressBarFactory; $this->_customerFactory = $customerFactory; $this->_customer = $customers; parent::__construct($name); } /** * @inheritDoc */ protected function configure() { parent::configure(); } /** * Execute the command * * @param InputInterface $input * @param OutputInterface $output * * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('<info>Execution starts.</info>'); $customercollection = $this->_customer->getCollection()->addAttributeToSelect("*")->load(); /** @var ProgressBar $progress */ $progressBar = $this->progressBarFactory->create( [ 'output' => $output, 'max' => count($customercollection), ] ); $progressBar->setFormat( 'Customer/Collection %current%/%max% %bar% %percent:3s%% %elapsed% %memory:6s%' ); $progressBar->start(); foreach ($customercollection as $customer) { /* Add your custom functionality here */ $progressBar->advance(); } $progressBar->finish(); $output->write(PHP_EOL); $output->writeln('<info>Execution ends.</info>'); return Cli::RETURN_SUCCESS; } }
Step 3: After that run the below commands
php bin/magento c:c php bin/magento c:f
Now, you need to run the below command in Magento root:
sudo php bin/magento magecomp:custom:progress-bar
The output will be as follows in the terminal after executing the above command.
Conclusion:
This way you can Add a Progress Bar using Custom CLI Command in Magento 2. Check out the list of useful CLI Commands. If you face any obstacles, let me know via the comment part. Share the article with your developer friends. Stay tuned with us for another Magento solution.
Happy Coding!