
In this tutorial we are going to show you how to get active payment methods list in Magento 2, This article will guide you how to get active payment methods in Magento 2 step by step.
Now let’s code!
Follow the below code to get active payment methods:
<?php namespace MageBeginners\Test\Model; use \Magento\Framework\App\Config\ScopeConfigInterface; use \Magento\Payment\Model\Config; class Paymentmethod extends \Magento\Framework\DataObject implements \Magento\Framework\Option\ArrayInterface { /** * @var ScopeConfigInterface */ protected $_appConfigScopeConfigInterface; /** * @var Config */ protected $_paymentModelConfig; /** * @param ScopeConfigInterface $appConfigScopeConfigInterface * @param Config $paymentModelConfig */ public function __construct( ScopeConfigInterface $appConfigScopeConfigInterface, Config $paymentModelConfig ) { $this->_appConfigScopeConfigInterface = $appConfigScopeConfigInterface; $this->_paymentModelConfig = $paymentModelConfig; } public function toOptionArray() { $payments = $this->_paymentModelConfig->getActiveMethods(); $methods = array(); foreach ($payments as $paymentCode => $paymentModel) { $paymentTitle = $this->_appConfigScopeConfigInterface ->getValue('payment/'.$paymentCode.'/title'); $methods[$paymentCode] = array( 'label' => $paymentTitle, 'value' => $paymentCode ); } return $methods; } }
$_appConfigScopeConfigInterface
is an object of Magento\Framework\App\Config\ScopeConfigInterface class.
$_paymentModelConfig
is an object of Magento\Payment\Model\Config class, and in toOptionArray()
function we use a method: getActiveMethods()
which returns model of all the active methods.
We hope this article helped you learn how to get active payment methods in Magento 2. You may also want to see our list of How to Change Favicon in Magento 2.
If you liked this article, then please subscribe to our Newsletter for Magento tutorials. You can also find us on Twitter and Facebook. If you have any problem or would like to add to the discussion, leave a comment below!