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!!!