Get Active Payment Methods with Multi select in Backend

Get Active Payment Methods with Multi select in Backend

If you are developing any payment related extension in Magento2 and you want to get the active Payment methods list with the multi selection option in Magento configuration,
You can follow this code.

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Payment\Model\Config;

class Paymethods implements \Magento\Framework\Option\ArrayInterface
{
protected $_appConfigScopeConfigInterface;

protected $_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;
}
}

Do let us know if you face any trouble or suggestions. We will always be happy to assist you.
Happy Coding.

Previous Article

Magento 1.x to Magento 2.x Migration

Next Article

Fixing: Failed to Load Resources Error After Installing Magento 2

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨