Today we are discussing how to get list of all payment methods which are active in Magento2.
To get this you can use following code:
<?php namespace Webkul\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; } }
Here,
$_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.
In return array we have all payment methods with their code and title.
I hope this blog will help you with Setup Script files in Magento 2. You may also check our wide range of best Magento 2 Extensions.
Please reach out to our team via a support ticket if you have any queries.
Try this and if you have any queries then just comment below 🙂
2 comments
We would be able to help you better if you please list your complete requirement at [email protected] and we can assist you accordingly.
Thanks and Regards
Webkul Team