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

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

2 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

3 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

6 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago