Magento2 : How to get active payment methods list store wise
Question
I want to get all active payment method list store wise.
I have two store
-- store indstore
-- store usstore
And i have set payment methods for only indstore and if i try to get all active methods ,it returns from default store settings and from default settings payment methods are disable.
if i enable from default settings than i got all payment methods.
Does anyone know how i can get methods store wise?
app\code\Vendor\Extension\Model\System\Methods.php
<?php
namespace Vendor\Extension\Model\System;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Model\Config;
class Methods implements \Magento\Framework\Option\ArrayInterface
{
protected $scopeConfig;
protected $paymentmodelconfig;
public function __construct(Config $paymentmodelconfig, ScopeConfigInterface $scopeConfig)
{
$this->paymentmodelconfig = $paymentmodelconfig;
$this->scopeConfig = $scopeConfig;
}
public function toOptionArray()
{
$payments = $this->paymentmodelconfig->getActiveMethods();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$methodList = $this->scopeConfig->getValue('payment',\Magento\Store\Model\ScopeInterface::SCOPE_STORE,1);
foreach ($methodList as $code => $_method) {
$active_status = "";
$title = "";
if (isset($_method['active']))
{
if ($_method['active'] == 1)
{
if (isset($_method['title']))
{
$title = $_method['title'];
$logger->info($title);
}
}
}
$methods = array();
return $methods;
}
}
}
Output IN Log File
2019-12-24T05:55:20+00:00 INFO (6): Check / Money order
Paypal is also active but i am getting only check/money order
Solution
class ABC{
protected $paymentMethodList;
public function __construct(
\Magento\Payment\Model\PaymentMethodList $paymentMethodList
) {
$this->paymentMethodList = $paymentMethodList;
}
public function getmethods()
{
$storeId = 'your store id';
$this->paymentMethodList->getActiveList($storeId);
}
}
OTHER TIPS
namespace VendorName\ModuleName\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.