How To

How to Add a Progress Bar using Custom CLI Command in Magento 2

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!

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

7 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

1 week ago