How To

How to Get List of Active Payment Methods Using Command Line?

Hello, Magento mates.

Welcome to MageComp Magento tutorials.

Today, we will learn to list active payment methods using command line in your Magento 2 store.

Magento 2 has undoubtedly helped many online store owners to take their business online and excel in the eCommerce world.

Magento 2 supports a variety of payment methods, including credit cards, PayPal, bank transfers, and more. These methods represent the various ways customers can make payments for their purchases on your Magento 2 store. When you configure and enable a payment method in the Magento 2 admin panel, it becomes active and visible to customers during the checkout process.

Steps to List Active Payment Methods Using Command Line

Step 1 –

First, we need to create “di.xml file” inside our Magento 2 extension by following the below path ?

{{magento_root}}/app/code/Vendor/Module/etc/

<?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\CommandList">
        <arguments>
            <argument name="commands" xsi:type="array">
                <item name="payment_list" xsi:type="object">Vendor\Module\Console\GetPaymentList</item>
            </argument>
        </arguments>
    </type>
</config>

Step 2 – 

Once you are done, you need to create GetPaymentList.php file inside our extension by following this path. ?

{{magento_root}}/app/code/Vendor/Module/Console

<?php
namespace Vendor\CustomCommand\Console;

use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;

class GetPaymentList extends Command
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */    private $scopeValue;

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeValue
     */    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeValue
    ) {
        $this->scopeValue = $scopeValue;
        parent::__construct();
    }

    /**
     * {@inheritdoc}
     */    protected function configure()
    {
        $this->setName('info:list:payment')
        ->setDescription('Here You can see Payment Method List')
        ->addArgument('status', InputArgument::OPTIONAL, 'Optional module name');
        parent::configure();
    }

    /**
     * {@inheritdoc}
     */    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $moduleName = (string) $input->getArgument('status');

        if ($moduleName == "Enabled" || $moduleName == "enabled")
        {
            return $this->showEnabledPaymentMethodList($output);
        }
        
        if ($moduleName != "Enabled" || $moduleName != "enabled" )
        {
            $output->writeln('<error>Please enter correct command</error>');
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }

    /**
     * Return enabled payment method list
     */    private function showEnabledPaymentMethodList(OutputInterface $output)
    {
        try {
            $table = new Table($output);
            $table->setHeaders(['Code', 'Name', 'Status']);
            $methodList = $this->scopeValue->getValue('payment');
            foreach ($methodList as $code => $_method)
            {
                $active_status = "";
                $title = "";

                if (isset($_method['active']))
                {
                    if ($_method['active'] == 1)
                    {
                        if (isset($_method['title']))
                        {
                            $title = $_method['title'];
                        }
                        $table->addRow([$code, $title, 'Enable']);
                    }
                }
            }
            $table->render();
            return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
        } catch (\Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE)
            {
                $output->writeln($e->getTraceAsString());
            }
            return \Magento\Framework\Console\Cli::RETURN_FAILURE;
        }
    }
}

Step 3 –

Once you have entered the code correctly, run the commands given below ?

php bin/magento info:list:payment enabled

Output ?

Final Say

And this is how it is done.

Magento 2 active payment methods are the payment options that are currently enabled and available for customers to use during the checkout process. These payment methods in Magento 2 play a crucial role in providing a smooth and diverse checkout experience for your customers by allowing them to choose the payment option that suits them best.

Hope you found this blog informative. If you have any queries about this blog or any Magento 2 feature, kindly contact us or text us on our official Facebook page, and if you have any trouble with your Magento 2 store, you can also hire our Magento developers at affordable rates.

Happy Coding!!!

Click to rate this post!
[Total: 0 Average: 0]
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

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

1 day ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

2 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

4 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

6 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

7 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

7 days ago