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/
1 2 3 4 5 6 7 8 9 10 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<?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 ?
1 |
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!!!